Home
C#
Dns Example
Updated Jun 1, 2023
Dot Net Perls
Dns. DNS servers resolve host names to IP addresses. With the System.Net namespace in the .NET Framework, we easily perform this task.
A useful method. The Dns.GetHostAddresses method converts a host name to its available IP addresses. An array of IPAddress instances is returned.
An example. Please add the System.Net namespace to the top of your program. We use a host name and pass it to the Dns.GetHostAddresses method. This returns an array of IPAddress classes.
Array
Tip We use the foreach-loop to enumerate the result array. We then call ToString to display our results.
foreach
using System; using System.Net; class Program { static void Main() { IPAddress[] array = Dns.GetHostAddresses("en.wikipedia.org"); foreach (IPAddress ip in array) { Console.WriteLine(ip.ToString()); } } }
2620:0:863:ed1a::1 198.35.26.96
Notes, program. The program prints the IP addresses for the English Wikipedia host. Try pasting the IP address into your web browser. It will be associated with Wikipedia.
A summary. We saw the Dns.GetHostAddresses method from the System.Net namespace. Sometimes it is useful to have the numeric IP address instead of relying on the host name.
For most programs, it is best to use the host name. The host name can be changed to resolve to a different IP address. This is more reliable.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
No updates found for this page.
Home
Changes
© 2007-2025 Sam Allen