System.Net.Dns.GetHostAddresses
Retrieves the IP addresses that are associated with a host name or IP address.
Syntax
public static System.Net.IPAddress[] GetHostAddresses(string hostNameOrAddress);
Parameters
| Name | Type | Description |
|---|---|---|
hostNameOrAddress |
string |
The DNS name or the IP address of the host to resolve. |
Return Value
System.Net.IPAddress[]
An array of type IPAddress that contains the IP addresses that are associated with the specified host. If the host name or IP address cannot be resolved, an empty array is returned.
Remarks
The GetHostAddresses method is used to resolve a host name or IP address into an array of IPAddress objects. This method can return multiple IP addresses for a single host name, including both IPv4 and IPv6 addresses.
If the hostNameOrAddress parameter is null, an empty array is returned.
This method performs a DNS lookup. Depending on network configuration, DNS caching, and server response times, this operation can take some time to complete.
GetHostAddressesAsync).
Examples
The following example demonstrates how to use the GetHostAddresses method to retrieve the IP addresses for a given host name:
using System;
using System.Net;
public class DnsExample
{
public static void Main(string[] args)
{
string hostName = "www.microsoft.com";
try
{
IPAddress[] addresses = Dns.GetHostAddresses(hostName);
Console.WriteLine($"IP Addresses for {hostName}:");
foreach (IPAddress address in addresses)
{
Console.WriteLine($"- {address}");
}
}
catch (SocketException ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}