Kasper Nielsen

Kasper Nielsen

  • NA
  • 4
  • 546

I am struggling understanding the code

Jun 23 2022 10:26 AM

Hello guys. 
I am new to C# and I am having trouble understanding it to be honest. 
I have this code that will get me the computername and IP-address of the computer its ran on but I have trouble understanding some of the code so I hope you guys can provide some help. 

"using System;
using System.Net;
namespace ComputernavnOgIpAdresse
{
    class Program
    {
        static void Main()
        {
            bool loop; /
            do
            {
                loop = true; 
                String strHostName = string.Empty;
                strHostName = Dns.GetHostName(); 


                Console.WriteLine("Din computers navn er : " + strHostName); 


                IPHostEntry ipEntry = Dns.GetHostEntry(strHostName); 

                IPAddress[] addr = ipEntry.AddressList; 

                for (int i = 1; i < addr.Length; i++)
                {

                    Console.WriteLine("Din computers IP-adresse er : " + addr[i].ToString()); 
                }
                Console.ReadLine();
            } while (loop==true);
        }
    }
}

"

The highlighted part in particular. What does this do? I am aware it gets me the IP-addresses but why? If I set the value of i = 0, it gives me both the IPv4 and IPv6 but if set to i = 1, I only get the IPv4 and I dont understand why. 

Thank you so much. 


Answers (2)