IPAddress Class
Namespace: System.Net
Inheritance: Object > MarshalByRefObject > IPAddress
Represents an Internet Protocol (IP) address.
This class is immutable. Once created, its value cannot be changed.
Constructors
-
IPAddress(Byte[] address)
Initializes a new instance of the
IPAddress
class with the specified byte array. -
IPAddress(Byte[] address, ULong scopeId)
Initializes a new instance of the
IPAddress
class with the specified byte array and scope identifier. -
IPAddress(String ipString)
Initializes a new instance of the
IPAddress
class using the specified string representation of an IP address.
Properties
-
AddressFamily AddressFamily
Gets the address family of the IP address.
-
ULong ScopeId
Gets the scope identifier for the IP address.
-
Boolean IsIPv4MappedToIPv6
Gets a value indicating whether the IP address is an IPv4-mapped IPv6 address.
-
Boolean IsIPv6
Gets a value indicating whether the IP address is an IPv6 address.
-
Boolean IsIPv6LinkLocal
Gets a value indicating whether the IP address is a link-local IPv6 address.
-
Boolean IsIPv6Multicast
Gets a value indicating whether the IP address is an IPv6 multicast address.
-
Boolean IsIPv6SiteLocal
Gets a value indicating whether the IP address is a site-local IPv6 address.
-
Boolean IsIPv6Teredo
Gets a value indicating whether the IP address is a Teredo-based IPv6 address.
-
Boolean IsLoopback
Gets a value indicating whether the IP address is a loopback address.
-
Boolean IsMatch(IPAddress address)
Determines whether the specified IP address is structurally equal to the current IP address.
Methods
-
Byte[] GetAddressBytes()
Returns the byte array representation of the IP address.
-
Int32 GetHashCode()
Returns the hash code for the current instance.
-
String ToString()
Returns the string representation of the IP address.
-
static IPAddress Parse(String ipString)
Converts an IP address string to an
IPAddress
instance. -
static Boolean TryParse(String ipString, out IPAddress address)
Attempts to convert the string representation of an IP address to an
IPAddress
instance.
Fields
-
static readonly IPAddress Any
Represents the IP address that is bound to all interfaces.
-
static readonly IPAddress Broadcast
Represents the IPv4 broadcast address.
-
static readonly IPAddress HostEntry
Represents the host IP address.
-
static readonly IPAddress IPv6Any
Represents the IPv6 address that is bound to all interfaces.
-
static readonly IPAddress IPv6Loopback
Represents the IPv6 loopback address.
-
static readonly IPAddress IPv6None
Represents the unspecified IPv6 address.
-
static readonly IPAddress Loopback
Represents the loopback IP address.
-
static readonly IPAddress None
Represents the unspecified IP address.
Example
The following code example demonstrates how to create and use IPAddress
objects.
using System;
using System.Net;
public class IPAddressExample
{
public static void Main(string[] args)
{
// Create an IPv4 address
IPAddress ipv4Address = IPAddress.Parse("192.168.1.1");
Console.WriteLine($"IPv4 Address: {ipv4Address}");
Console.WriteLine($"Address Family: {ipv4Address.AddressFamily}");
Console.WriteLine($"Is IPv4 Loopback: {ipv4Address.IsLoopback}");
Console.WriteLine();
// Create an IPv6 address
IPAddress ipv6Address = IPAddress.Parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
Console.WriteLine($"IPv6 Address: {ipv6Address}");
Console.WriteLine($"Address Family: {ipv6Address.AddressFamily}");
Console.WriteLine($"Is IPv6 Loopback: {ipv6Address.IsLoopback}");
Console.WriteLine();
// Using TryParse
IPAddress parsedAddress;
if (IPAddress.TryParse("10.0.0.1", out parsedAddress))
{
Console.WriteLine($"Successfully parsed: {parsedAddress}");
}
else
{
Console.WriteLine("Failed to parse IP address.");
}
// Accessing static properties
Console.WriteLine($"Any IP Address: {IPAddress.Any}");
Console.WriteLine($"Loopback IP Address: {IPAddress.Loopback}");
}
}
Remarks
The IPAddress
class is used to represent an IP address (either IPv4 or IPv6). It is fundamental for network programming in .NET, allowing applications to specify which network interfaces to listen on or which remote hosts to connect to.
Key features include:
- Immutable: Once an
IPAddress
object is created, its value cannot be changed. - IPv4 and IPv6 Support: The class handles both IPv4 and IPv6 address families.
- Static Properties: Provides convenient access to commonly used IP addresses like
Any
,Loopback
, andBroadcast
. - Parsing: Methods like
Parse
andTryParse
allow for easy conversion of string representations toIPAddress
objects.
When dealing with IPv6 addresses, it's important to be aware of scope identifiers, especially for link-local addresses. The ScopeId
property and related constructors are used for this purpose.