System.Net.IPAddress Class

Provides an IP address.

Namespace:

System.Net

Assembly:

System (in System.dll)

Syntax

public class IPAddress

Remarks

The IPAddress class represents an IP address. It can represent either an IPv4 address or an IPv6 address.

An IP address is a logical address that identifies a network interface on a network. Computers use IP addresses to communicate with each other.

Use the methods of the IPAddress class to create and manipulate IP addresses. For example, you can use the static members IPAddress.Parse to convert a string representation of an IP address to an IPAddress object, or IPAddress.ToString to convert an IPAddress object to its string representation.

Note

The IPAddress class can represent both IPv4 and IPv6 addresses. To specify the address family (IPv4 or IPv6), use the AddressFamily enumeration.

Constructor Summary

Constructor Description
IPAddress(byte[]) Initializes a new instance of the IPAddress class with the specified byte array.
IPAddress(long) Initializes a new instance of the IPAddress class using the specified unsigned integer representation of the host.

Fields

The IPAddress class does not expose any public fields.

Properties Summary

Property Description
AddressBytes Gets the byte array representation of the IP address.
AddressFamily Gets the AddressFamily of the IP address.
IsCertificateHashString Gets a value that indicates whether the IP address is a certificate hash string.
IsIPv4MappedToIPv6 Gets a value that indicates whether the IP address is an IPv4-mapped IPv6 address.
IsLoopback Gets a value that indicates whether the IP address is a loopback address.
IsMulticast Gets a value that indicates whether the IP address is a multicast address.
IsRecognized Gets a value that indicates whether the IP address is a recognized IP address.
IsUnspecified Gets a value that indicates whether the IP address is an unspecified address.

Methods Summary

Method Description
Equals(object) Determines whether the specified object is equal to the current object.
GetHashCode() Serves as the default hash function.
GetType() Gets the type of the current instance.
Parse(string) Converts an IPv4 or IPv6 address from its string representation to an IPAddress instance.
ToString() Returns a string representation of the IP address.
TryParse(string, out IPAddress) Converts the string representation of an IP address to its IPAddress equivalent. A return value indicates whether the conversion succeeded.
TryParse(string, out IPAddress, out AddressFamily) Converts the string representation of an IP address to its IPAddress equivalent and determines the address family. A return value indicates whether the conversion succeeded.

Static Members

Fields

The IPAddress class does not expose any public static fields.

Properties

Property Description
IPv4Any Represents the wildcard IP address (0.0.0.0).
IPv4Broadcast Represents the broadcast IP address (255.255.255.255).
IPv4Loopback Represents the loopback IP address (127.0.0.1).
IPv6Any Represents the wildcard IPv6 address (::).
IPv6Loopback Represents the loopback IPv6 address (::1).
IPv6None Represents the unspecified IPv6 address (::).

Methods

See Parse(string) and TryParse(string, out IPAddress) methods for converting string representations to IPAddress objects.

Constructor

public IPAddress(byte[] address)

Initializes a new instance of the IPAddress class with the specified byte array.

Parameters:

  • address: An array of bytes that represents the IP address.

Constructor

public IPAddress(long newAddress)

Initializes a new instance of the IPAddress class using the specified unsigned integer representation of the host.

Parameters:

  • newAddress: A 64-bit signed integer that contains the host IP address.

Property

public byte[] AddressBytes { get; }

Gets the byte array representation of the IP address.

Property

public AddressFamily AddressFamily { get; }

Gets the AddressFamily of the IP address.

Property

public bool IsCertificateHashString { get; }

Gets a value that indicates whether the IP address is a certificate hash string.

Property

public bool IsIPv4MappedToIPv6 { get; }

Gets a value that indicates whether the IP address is an IPv4-mapped IPv6 address.

Property

public bool IsLoopback { get; }

Gets a value that indicates whether the IP address is a loopback address.

Property

public bool IsMulticast { get; }

Gets a value that indicates whether the IP address is a multicast address.

Property

public bool IsRecognized { get; }

Gets a value that indicates whether the IP address is a recognized IP address.

Property

public bool IsUnspecified { get; }

Gets a value that indicates whether the IP address is an unspecified address.

Method

public override bool Equals(object obj)

Serves as the default hash function.

Method

public override int GetHashCode()

Serves as the default hash function.

Method

public Type GetType()

Gets the type of the current instance.

Method

public static IPAddress Parse(string ipString)

Converts an IPv4 or IPv6 address from its string representation to an IPAddress instance.

Parameters:

  • ipString: The string to convert.

Returns: An IPAddress instance.

Throws:

  • ArgumentNullException: ipString is null.
  • FormatException: ipString is not a valid IP address.

Method

public override string ToString()

Returns a string representation of the IP address.

Returns: A string representation of the IP address.

Method

public static bool TryParse(string ipString, out IPAddress address)

Converts the string representation of an IP address to its IPAddress equivalent. A return value indicates whether the conversion succeeded.

Parameters:

  • ipString: The string to convert.
  • address: When this method returns, contains an IPAddress instance equivalent to the ipString, if the conversion succeeded, or null if the conversion failed.

Returns: true if ipString was converted successfully; otherwise, false.

Method

public static bool TryParse(string ipString, out IPAddress address, out AddressFamily family)

Converts the string representation of an IP address to its IPAddress equivalent and determines the address family. A return value indicates whether the conversion succeeded.

Parameters:

  • ipString: The string to convert.
  • address: When this method returns, contains an IPAddress instance equivalent to the ipString, if the conversion succeeded, or null if the conversion failed.
  • family: When this method returns, contains the AddressFamily of the parsed IP address if the conversion succeeded, or AddressFamily.Unspecified if the conversion failed.

Returns: true if ipString was converted successfully; otherwise, false.

Keywords:

System.Net IPAddress network address IPv4 IPv6 socket