IPAddress Class
Represents an Internet Protocol (IP) address.
Namespace: System.Net
Assembly: System.Net.Primitives (in System.Net.Primitives.dll)
Syntax
public sealed class IPAddress
Fields
| Name |
Description |
Any |
Represents the IP address that listens on all network interfaces. |
Broadcast |
Represents the IP broadcast address. |
HostEntry |
Represents the local host address. |
Loopback |
Represents the loopback address. |
None |
Represents an invalid IP address. |
Constructors
| Signature |
Description |
IPAddress(byte[] address) |
Initializes a new instance of the IPAddress class with the specified byte array representation of the IP address. |
IPAddress(long newAddress) |
Initializes a new instance of the IPAddress class with the specified unsigned integer representation of the IPv4 address. |
Properties
| Name |
Description |
AddressFamily |
Gets the address family of the IP address. |
IsIPv4MappedToIPv6 |
Gets a value indicating whether the IP address is an IPv4-mapped IPv6 address. |
IsLoopback |
Gets a value indicating whether the IP address is a loopback address. |
Methods
| Signature |
Description |
Equals(object obj) |
Determines whether the specified object is equal to the current object. |
GetAddressBytes() |
Returns the byte array representation of the IP address. |
GetHashCode() |
Serves as the default hash function. |
Parse(string ipString) |
Converts or copies the dotted-decimal string representation of an IPv4 address to a 32-bit unsigned integer. |
ToString() |
Returns the string representation of the IP address. |
TryCreate(string ipString, out IPAddress? address) |
Tries to convert the string representation of an IP address to an IPAddress instance. |
Examples
Creating an IPv4 Address
using System.Net;
IPAddress ipv4Address = IPAddress.Parse("192.168.1.1");
Console.WriteLine($"IPv4 Address: {ipv4Address}");
byte[] ipv4Bytes = { 10, 0, 0, 1 };
IPAddress ipv4FromBytes = new IPAddress(ipv4Bytes);
Console.WriteLine($"IPv4 from Bytes: {ipv4FromBytes}");
byte[] addressBytes = ipv4Address.GetAddressBytes();
Console.WriteLine($"Address Bytes: {string.Join(".", addressBytes)}");
Creating an IPv6 Address
using System.Net;
IPAddress ipv6Address = IPAddress.Parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
Console.WriteLine($"IPv6 Address: {ipv6Address}");
byte[] ipv6Bytes = { 0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34 };
IPAddress ipv6FromBytes = new IPAddress(ipv6Bytes);
Console.WriteLine($"IPv6 from Bytes: {ipv6FromBytes}");
Console.WriteLine($"Is Loopback: {ipv6Address.IsLoopback}");
Using Special IP Addresses
using System.Net;
Console.WriteLine($"Any IP Address: {IPAddress.Any}");
Console.WriteLine($"Loopback IP Address: {IPAddress.Loopback}");
Console.WriteLine($"Broadcast IP Address: {IPAddress.Broadcast}");
Trying to Create an IP Address
using System.Net;
if (IPAddress.TryParse("172.16.0.1", out IPAddress? address))
{
Console.WriteLine($"Successfully parsed: {address}");
}
else
{
Console.WriteLine("Failed to parse the IP address.");
}
if (IPAddress.TryParse("invalid-ip", out IPAddress? invalidAddress))
{
Console.WriteLine($"Successfully parsed: {invalidAddress}");
}
else
{
Console.WriteLine("Failed to parse 'invalid-ip'.");
}