.NET Networking API Documentation

Class System.Net.IPAddress

Represents an Internet Protocol (IP) address.

The IPAddress class provides a managed representation of an IP address. It can represent both IPv4 and IPv6 addresses.

Overview

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.

Constructors

IPAddress(byte[] address)

Initializes a new instance of the IPAddress class with the specified byte array representation of an IP address.

  • Parameters: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.

  • Parameters:newAddress - The unsigned integer representation of the IPv4 address.

Properties

AddressFamily

Gets the address family of the IPAddress instance.

IsIPv4MappedToIPv6

Gets a value indicating whether the IPAddress is an IPv4-mapped IPv6 address.

  • Returns: true if the address is an IPv4-mapped IPv6 address; otherwise, false.

IsIPv6LinkLocal

Gets a value indicating whether the IPAddress is an IPv6 link-local address.

  • Returns: true if the address is an IPv6 link-local address; otherwise, false.

IsIPv6Multicast

Gets a value indicating whether the IPAddress is an IPv6 multicast address.

  • Returns: true if the address is an IPv6 multicast address; otherwise, false.

IsIPv6SiteLocal

Gets a value indicating whether the IPAddress is an IPv6 site-local address.

  • Returns: true if the address is an IPv6 site-local address; otherwise, false.

IsIPv6Teredo

Gets a value indicating whether the IPAddress is an IPv6 Teredo address.

  • Returns: true if the address is an IPv6 Teredo address; otherwise, false.

Static Properties

Any

Gets an IPAddress instance that is bound to the IP address that includes any available network interface.

  • Returns: An IPAddress instance bound to any IP address.

Broadcast

Gets an IPAddress instance that is bound to the IPv4 broadcast address.

  • Returns: An IPAddress instance representing the IPv4 broadcast address.

HostName

Gets the host name of the local computer.

  • Returns: A string containing the host name of the local computer.

Loopback

Gets an IPAddress instance that is bound to the loopback IPv4 address.

  • Returns: An IPAddress instance representing the loopback IPv4 address.

Static Methods

Parse(string ipString)

Converts an String into an IPAddress instance.

  • Parameters:ipString - The string to convert.
  • Returns: An IPAddress instance.
  • Throws: FormatException - ipString is not a valid IP address.

TryParse(string ipString, out IPAddress address)

Tries to convert a string into an IPAddress instance.

  • Parameters: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.
  • Returns: true if ipString was converted successfully; otherwise, false.

Methods

Equals(object obj)

Determines whether two IP address instances are equal.

  • Parameters:obj - The object to compare with the current instance.
  • Returns: 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.

  • Returns: A byte array containing the IP address.

GetHashCode()

Returns the hash code for this IPAddress instance.

  • Returns: A 32-bit signed integer that is the hash code for this instance.

ToString()

Returns a string representation of the IP address.

  • Returns: A string representing the IP address.

Enumeration System.Net.Sockets.AddressFamily

Defines the address family used by the IPAddress class.

Members

  • InterNetwork: Indicates the IPv4 address family.
  • InterNetworkV6: Indicates the IPv6 address family.
  • Unknown: Indicates an unknown address family.

Example Usage


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.");
        }
    }
}