UnicastIpAddressInformation Class
Represents information about a unicast IP address.
Namespace
System.Net.IP
Assembly
System.Net.Sockets.dll
Syntax
public class UnicastIpAddressInformation : IpAddressInformation
Remarks
The UnicastIpAddressInformation
class provides details about a unicast IP address, including its address, prefix length, whether it's a unicast address, and if it's a deprecated or duplicate address.
This class is used by the GetUnicastAddresses
method of the NetworkInterface
class to retrieve information about the unicast addresses assigned to a network interface.
Inheritance
UnicastIpAddressInformation
inherits from IpAddressInformation
.
Fields
This class has no fields.
Properties
Address: IPAddress
Gets the IP address.
IsDnsEligible: bool
Gets a value indicating whether the IP address can be used for DNS.
IsTransient: bool
Gets a value indicating whether the IP address is transient.
PrefixLength: int
Gets the prefix length of the IP address.
IsWellKnown: bool
Gets a value indicating whether the IP address is a well-known address.
IsReserved: bool
Gets a value indicating whether the IP address is reserved.
IsTemporary: bool
Gets a value indicating whether the IP address is temporary.
IsUnicast: bool
Gets a value indicating whether the IP address is a unicast address.
IsAny: bool
Gets a value indicating whether the IP address represents any address.
IsBroadcast: bool
Gets a value indicating whether the IP address is a broadcast address.
IsLoopback: bool
Gets a value indicating whether the IP address is a loopback address.
IsMulticast: bool
Gets a value indicating whether the IP address is a multicast address.
Methods
ToString(): string
Returns the string representation of the IP address.
Example
C# Example
The following example demonstrates how to retrieve and display unicast IP address information for all network interfaces on the system.
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)
{
Console.WriteLine($"Network Interface: {adapter.Name} ({adapter.Description})");
IPInterfaceProperties ipProps = adapter.GetIPProperties();
UnicastIPAddressInformationCollection uniCast = ipProps.UnicastAddresses;
if (uniCast.Count > 0)
{
Console.WriteLine(" Unicast Addresses:");
foreach (UnicastIPAddressInformation item in uniCast)
{
Console.WriteLine($" Address: {item.Address}");
Console.WriteLine($" Prefix Length: {item.PrefixLength}");
Console.WriteLine($" Is Dns Eligible: {item.IsDnsEligible}");
Console.WriteLine($" Is Transient: {item.IsTransient}");
Console.WriteLine($" Is Well-Known: {item.IsWellKnown}");
Console.WriteLine($" Is Reserved: {item.IsReserved}");
Console.WriteLine($" Is Temporary: {item.IsTemporary}");
Console.WriteLine($" Is Unicast: {item.IsUnicast}");
Console.WriteLine($" Is Any: {item.IsAny}");
Console.WriteLine($" Is Broadcast: {item.IsBroadcast}");
Console.WriteLine($" Is Loopback: {item.IsLoopback}");
Console.WriteLine($" Is Multicast: {item.IsMulticast}");
Console.WriteLine();
}
}
else
{
Console.WriteLine(" No unicast addresses configured.");
}
Console.WriteLine("--------------------------------------------------");
}
}
}