IPAddressFamily Enumeration
The IPAddressFamily
enumeration represents the address families that can be used for IP addressing.
This enumeration is used by the System.Net.Sockets
namespace to specify the type of IP addresses that network protocols will use.
Namespace:
System.Net
Syntax:
public enum IPAddressFamily
Members:
The following members are defined for the IPAddressFamily
enumeration:
Member | Description |
---|---|
InterNetwork |
Represents the IPv4 address family. |
InterNetworkV6 |
Represents the IPv6 address family. |
Unknown |
Represents an unknown address family. |
Remarks:
The IPAddressFamily
enumeration is fundamental to network programming in .NET.
When creating network sockets or working with IP addresses, you often need to specify whether you are dealing with IPv4 or IPv6.
- Use
InterNetwork
when working with IPv4 addresses (e.g.,192.168.1.1
). - Use
InterNetworkV6
when working with IPv6 addresses (e.g.,2001:0db8:85a3:0000:0000:8a2e:0370:7334
). - The
Unknown
value is typically used when the address family cannot be determined or is not applicable.
Example:
The following C# code snippet demonstrates how to check the address family of an IP address:
using System;
using System.Net;
public class IPAddressFamilyExample
{
public static void Main(string[] args)
{
// Example IPv4 address
IPAddress ipv4Address = IPAddress.Parse("192.168.1.100");
Console.WriteLine($"Address: {ipv4Address}");
Console.WriteLine($"Family: {ipv4Address.AddressFamily}"); // Output: InterNetwork
Console.WriteLine();
// Example IPv6 address
IPAddress ipv6Address = IPAddress.Parse("2001:db8::1");
Console.WriteLine($"Address: {ipv6Address}");
Console.WriteLine($"Family: {ipv6Address.AddressFamily}"); // Output: InterNetworkV6
Console.WriteLine();
// Checking a specific family
if (ipv4Address.AddressFamily == IPAddressFamily.InterNetwork)
{
Console.WriteLine("This is an IPv4 address.");
}
if (ipv6Address.AddressFamily == IPAddressFamily.InterNetworkV6)
{
Console.WriteLine("This is an IPv6 address.");
}
}
}