Uri.CheckHostName(String) Method in C#

The Uri.CheckHostName() method in C# is used to determine the type of hostname specified in a string. It validates whether the given string represents a valid DNS name, IPv4 address, IPv6 address, or an unknown format.

Syntax

Following is the syntax −

public static UriHostNameType CheckHostName(string hostName);

Parameters

  • hostName − A string that contains the hostname to validate.

Return Value

The method returns a UriHostNameType enumeration value that indicates the type of the hostname −

UriHostNameType Description
Dns Valid DNS hostname
IPv4 Valid IPv4 address
IPv6 Valid IPv6 address
Basic Valid basic hostname (NetBIOS name)
Unknown Invalid or unrecognized hostname format

Using CheckHostName() with DNS Names

Example

using System;

public class Demo {
   public static void Main() {
      string hostname = "www.tutorialspoint.com";
      Console.WriteLine("Hostname = " + hostname);
      UriHostNameType result = Uri.CheckHostName(hostname);
      Console.WriteLine("Host type = " + result);
      
      string localhost = "localhost";
      Console.WriteLine("\nHostname = " + localhost);
      result = Uri.CheckHostName(localhost);
      Console.WriteLine("Host type = " + result);
   }
}

The output of the above code is −

Hostname = www.tutorialspoint.com
Host type = Dns

Hostname = localhost
Host type = Dns

Using CheckHostName() with IP Addresses

Example

using System;

public class Demo {
   public static void Main() {
      string ipv4 = "192.168.1.1";
      Console.WriteLine("IP Address = " + ipv4);
      UriHostNameType result = Uri.CheckHostName(ipv4);
      Console.WriteLine("Host type = " + result);
      
      string ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334";
      Console.WriteLine("\nIP Address = " + ipv6);
      result = Uri.CheckHostName(ipv6);
      Console.WriteLine("Host type = " + result);
      
      string invalidHost = "http://localhost";
      Console.WriteLine("\nInvalid hostname = " + invalidHost);
      result = Uri.CheckHostName(invalidHost);
      Console.WriteLine("Host type = " + result);
   }
}

The output of the above code is −

IP Address = 192.168.1.1
Host type = IPv4

IP Address = 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Host type = IPv6

Invalid hostname = http://localhost
Host type = Unknown

Practical Usage Example

Example

using System;

public class HostnameValidator {
   public static void ValidateHostname(string hostname) {
      UriHostNameType hostType = Uri.CheckHostName(hostname);
      
      switch (hostType) {
         case UriHostNameType.Dns:
            Console.WriteLine($"{hostname} is a valid DNS name");
            break;
         case UriHostNameType.IPv4:
            Console.WriteLine($"{hostname} is a valid IPv4 address");
            break;
         case UriHostNameType.IPv6:
            Console.WriteLine($"{hostname} is a valid IPv6 address");
            break;
         case UriHostNameType.Basic:
            Console.WriteLine($"{hostname} is a basic hostname");
            break;
         default:
            Console.WriteLine($"{hostname} is not a valid hostname");
            break;
      }
   }
   
   public static void Main() {
      ValidateHostname("google.com");
      ValidateHostname("127.0.0.1");
      ValidateHostname("::1");
      ValidateHostname("invalid..host");
   }
}

The output of the above code is −

google.com is a valid DNS name
127.0.0.1 is a valid IPv4 address
::1 is a valid IPv6 address
invalid..host is not a valid hostname

Conclusion

The Uri.CheckHostName() method is essential for validating hostname formats in network applications. It efficiently identifies DNS names, IPv4/IPv6 addresses, and invalid formats, making it useful for input validation and URL processing scenarios.

Updated on: 2026-03-17T07:04:35+05:30

479 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements