close
C# Online.NET Visual C# Developer Center
Search

Get command line arguments

C# Code Snippets

C# Source Code Bank

edit

This C# code snippet obtains a command line argument and converts it to a number in the Main method.

public static void Main (string[] args)
{
   if (args.Length > 0)
   {
      System.Console.WriteLine (args[0]);
      try
      {
//       long number = Int64.Parse(args[0]);
//       long number = Convert.ToInt64(s);
         long number = long.Parse (args[0]);
         System.Console.WriteLine ("Number = " + number);
      }
      catch (System.FormatException e)
      {
         System.Console.WriteLine ("FormatException" + e);
      }
      return;
   }
   System.Console.WriteLine ("Please enter a number");
}

Visual C# Best Practices

Always put format conversions inside try-catch blocks.