System.Net.IPAddressRepresents an Internet Protocol (IP) address.
The IPAddress class provides a managed representation of an IP address. It can represent both IPv4 and IPv6 addresses.
This class is fundamental for network programming in .NET. It allows you to create, manipulate, and compare IP addresses, and to determine whether an address is IPv4 or IPv6.
IPAddress(byte[] address)Initializes a new instance of the IPAddress class with the specified byte array representation of an IP address.
address - A byte array that contains the IP address.IPAddress(long newAddress)Initializes a new instance of the IPAddress class with the specified unsigned integer representation of an IPv4 address.
newAddress - The unsigned integer representation of the IPv4 address.AddressFamilyGets the address family of the IPAddress instance.
AddressFamily enumeration value.IsIPv4MappedToIPv6Gets a value indicating whether the IPAddress is an IPv4-mapped IPv6 address.
true if the address is an IPv4-mapped IPv6 address; otherwise, false.IsIPv6LinkLocalGets a value indicating whether the IPAddress is an IPv6 link-local address.
true if the address is an IPv6 link-local address; otherwise, false.IsIPv6MulticastGets a value indicating whether the IPAddress is an IPv6 multicast address.
true if the address is an IPv6 multicast address; otherwise, false.IsIPv6SiteLocalGets a value indicating whether the IPAddress is an IPv6 site-local address.
true if the address is an IPv6 site-local address; otherwise, false.IsIPv6TeredoGets a value indicating whether the IPAddress is an IPv6 Teredo address.
true if the address is an IPv6 Teredo address; otherwise, false.AnyGets an IPAddress instance that is bound to the IP address that includes any available network interface.
IPAddress instance bound to any IP address.BroadcastGets an IPAddress instance that is bound to the IPv4 broadcast address.
IPAddress instance representing the IPv4 broadcast address.HostNameGets the host name of the local computer.
LoopbackGets an IPAddress instance that is bound to the loopback IPv4 address.
IPAddress instance representing the loopback IPv4 address.Parse(string ipString)Converts an String into an IPAddress instance.
ipString - The string to convert.IPAddress instance.FormatException - ipString is not a valid IP address.TryParse(string ipString, out IPAddress address)Tries to convert a string into an IPAddress instance.
ipString - The string to convert.address - When this method returns, contains an IPAddress instance equivalent to the IP address or network mask contained in ipString, if the conversion succeeded, or null if the conversion failed.true if ipString was converted successfully; otherwise, false.Equals(object obj)Determines whether two IP address instances are equal.
obj - The object to compare with the current instance.true if the specified object is an IPAddress instance equal to the current instance; otherwise, false.GetAddressBytes()Returns the byte array representation of the IP address.
GetHashCode()Returns the hash code for this IPAddress instance.
ToString()Returns a string representation of the IP address.
System.Net.Sockets.AddressFamilyDefines the address family used by the IPAddress class.
InterNetwork: Indicates the IPv4 address family.InterNetworkV6: Indicates the IPv6 address family.Unknown: Indicates an unknown address family.
using System;
using System.Net;
public class Example
{
public static void Main(string[] args)
{
// Create an IPv4 address
IPAddress ipv4Address = IPAddress.Parse("192.168.1.1");
Console.WriteLine($"IPv4 Address: {ipv4Address.ToString()}");
Console.WriteLine($"Address Family: {ipv4Address.AddressFamily}");
// Create an IPv6 address
IPAddress ipv6Address = IPAddress.Parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
Console.WriteLine($"IPv6 Address: {ipv6Address.ToString()}");
Console.WriteLine($"Address Family: {ipv6Address.AddressFamily}");
// Check if an address is loopback
if (IPAddress.IsLoopback(ipv4Address))
{
Console.WriteLine("The IPv4 address is a loopback address.");
}
// Try to parse a string
IPAddress parsedAddress;
if (IPAddress.TryParse("10.0.0.5", out parsedAddress))
{
Console.WriteLine($"Successfully parsed: {parsedAddress}");
}
else
{
Console.WriteLine("Failed to parse the IP address.");
}
}
}