IPAddressInfo Class

Namespace: System.Net.IP
Provides information about an IP address, including its scope ID. This class is typically used when working with IPv6 addresses.

Remarks

The IPAddressInfo class is part of the System.Net.IP namespace and offers a way to encapsulate and access details of an IP address. It's particularly useful for IPv6 addresses where the concept of a scope ID is relevant for distinguishing between interfaces on a network.

Instances of this class are often returned by methods that query network interfaces or resolve hostnames.

Fields

No public fields are directly exposed. Information is accessed via properties.

Properties

Name Description
AddressFamily Gets the address family of the IP address (e.g., IPv4 or IPv6).
IsIPv6SiteLocalAddress Gets a value indicating whether the IP address is a site-local IPv6 address.
IsWellKnown Gets a value indicating whether the IP address is a well-known multicast address.
IsTransient Gets a value indicating whether the IP address is a transient address.
IsLocalHostAddress Gets a value indicating whether the IP address is the loopback address.
IsMulticastAddress Gets a value indicating whether the IP address is a multicast address.
IsUnspecifiedAddress Gets a value indicating whether the IP address is an unspecified address (e.g., 0.0.0.0 or ::).
ScopeId Gets the scope ID associated with the IP address. This is particularly relevant for IPv6 addresses to specify the interface.
Address Gets the IP address represented by this instance.

Methods

Name Description
ToString() Returns the string representation of the IP address.
GetAddressBytes() Returns the raw binary representation of the IP address.

Constructors

This class does not have public constructors. Instances are typically obtained from other methods.

Example

The following C# code snippet demonstrates how to retrieve and display information about IP addresses on the local machine.
using System;
using System.Net;
using System.Net.NetworkInformation;

public class IpInfoExample
{
    public static void Main(string[] args)
    {
        NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

        foreach (NetworkInterface adapter in networkInterfaces)
        {
            IPInterfaceProperties ipProps = adapter.GetIPProperties();
            IPAddressInformationCollection ipCollection = ipProps.UnicastAddresses;

            Console.WriteLine($"Interface: {adapter.Name} ({adapter.Description})");

            foreach (UnicastIPAddressInformation ipInfo in ipCollection)
            {
                if (ipInfo.Address.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    // IPAddressInfo can be implicitly converted from UnicastIPAddressInformation
                    IPAddressInfo ipv6Info = ipInfo; 

                    Console.WriteLine($"  IPv6 Address: {ipv6Info.Address}");
                    Console.WriteLine($"    Scope ID: {ipv6Info.ScopeId}");
                    Console.WriteLine($"    Is Site Local: {ipv6Info.IsIPv6SiteLocalAddress}");
                    Console.WriteLine($"    Is Multicast: {ipv6Info.IsMulticastAddress}");
                    Console.WriteLine($"    Is Loopback: {ipv6Info.IsLocalHostAddress}");
                    Console.WriteLine($"    Is Transient: {ipv6Info.IsTransient}");
                }
                else if (ipInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                {
                    Console.WriteLine($"  IPv4 Address: {ipInfo.Address}");
                    Console.WriteLine($"    Is Loopback: {ipInfo.IsLocalHostAddress}");
                    Console.WriteLine($"    Is Multicast: {ipInfo.IsMulticastAddress}");
                }
            }
            Console.WriteLine();
        }
    }
}

Requirements

Requirement Details
.NET Framework Supported in: .NET Framework 4.5, .NET Framework 4.0, .NET Framework 3.5, .NET Framework 3.0, .NET Framework 2.0
.NET Core/.NET 5+
Namespace System.Net.IP
Assembly System.Net.Primitives.dll