Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     public static string ResolveHost(string hostName)
     {
         try
         {
             IPAddress[] address = Dns.GetHostAddresses(hostName);

             if (address.Length == 1)
             {
                 return address[0].ToString();
             }

             // if we got more addresses, try to pick a IPv4 one
             for (int index = 0; index < address.Length; index++)
             {
                 IPAddress ipAddress = address[index];
                 if (ipAddress != null)
                 {
                     string ipString = ipAddress.ToString();
                     if (ipString.IndexOf('.') >= 0)
                     {
                         return ipString;
                     }
                 }
             }
         }
         catch (System.Exception e)
         {
             Debug.Log("Exception caught! " + e.Source + " Message: " + e.Message);
         }

         return String.Empty;
     }