IPAddress Class

Represents a Internet Protocol (IP) address.
Namespace: System.Net.Sockets
IPAddress (Inherits from Object)

Public Constructors

IPAddress(Byte[])
Initializes a new instance of the IPAddress class with the specified byte array.
IPAddress(Byte[], ULong)
Initializes a new instance of the IPAddress class with the specified byte array and scope ID.

Public Static Properties

Any
Gets the IPAddress that represents any address.
Broadcast
Gets the IPAddress that represents the broadcast address.
Loopback
Gets the IPAddress that represents the loopback address.
None
Gets the IPAddress that represents an unspecified address.
IPv6Any
Gets the IPAddress that represents any IPv6 address.
IPv6Loopback
Gets the IPAddress that represents the IPv6 loopback address.
IPv6None
Gets the IPAddress that represents an unspecified IPv6 address.

Public Static Methods

Parse(String)
Converts an IPAddress string to an IPAddress instance.
TryParse(String, out IPAddress)
Converts the specified string representation of an IP address to an IPAddress instance. A return value indicates whether the conversion succeeded or failed.
IsLoopback(IPAddress)
Returns a value indicating whether the specified IPAddress is a loopback address.

Public Instance Properties

AddressFamily
Gets the Internet Protocol version of the IPAddress.
ScopeId
Gets the scope ID of the IPAddress.

Public Instance Methods

Equals(Object)
Determines whether the specified object is equal to the current object.
GetAddressBytes()
Returns the byte array representation of the IPAddress.
GetHashCode()
Serves as the default hash function.
ToString()
Returns a string representation of the IPAddress.

Public Methods Inherited from Object

Syntax

public sealed class IPAddress : ...

Remarks

The IPAddress class represents an IP address. It can be used to store both IPv4 and IPv6 addresses. This class is immutable.

Common IP addresses like loopback, broadcast, and any address are available as static properties for convenience.

Example

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}");
        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}");
        Console.WriteLine($"Address Family: {ipv6Address.AddressFamily}");

        // Check if an address is loopback
        Console.WriteLine($"Is 127.0.0.1 loopback? {IPAddress.IsLoopback(IPAddress.Parse("127.0.0.1"))}");
    }
}