Mohanapriya R Mohanapriya R
Updated date Jul 01, 2024
In this article, we will explore DNS by converting DNS information into strings using C#.

Domain Name System (DNS) is like the phonebook of the internet, mapping domain names to IP addresses. It uses a hierarchical structure, and each level in the hierarchy represents a different domain. For example, "www.techiehook.com" has three levels: "www" (subdomain), "techiehook" (domain), and "com" (top-level domain). Converting DNS information into strings helps in better representation and manipulation of these details.

C# Program for DNS to String Conversion:

using System;
using System.Net;

class Program
{
    static void Main()
    {
        // Example DNS information
        IPAddress[] ipAddressList = Dns.GetHostAddresses("www.techiehook.com");

        // Convert DNS information to string
        string dnsString = ConvertDnsToString(ipAddressList);

        // Display the result
        Console.WriteLine("DNS Information as String:");
        Console.WriteLine(dnsString);
    }

    static string ConvertDnsToString(IPAddress[] ipAddressList)
    {
        // Concatenate IP addresses into a string
        string result = "IP Addresses: ";
        foreach (IPAddress ipAddress in ipAddressList)
        {
            result += ipAddress.ToString() + ", ";
        }

        // Remove the trailing comma and space
        result = result.TrimEnd(',', ' ');

        return result;
    }
}

Output:

DNS Information as String:
IP Addresses: 91.185.218.34

In this example, we use the Dns.GetHostAddresses method to obtain the IP addresses associated with the provided domain name ("www.techiehook.com"). The program then uses the ConvertDnsToString method to convert the array of IP addresses into a string, which is displayed as output.

Comments (0)

There are no comments. Be the first to comment!!!