← All Articles

How to Read Program Arguments in C#

Posted on

In C# program arguments (like in most c-based languages) are a part of the main method signature. Like in java this variable is an array of strings.

class Hello
{
  static void Main(string[] args)
  {
    if (args == null || args.Length < 2) {
      throw new System.ArgumentException(String.Format("Usage: {0} name [name] [name] [name]",System.AppDomain.CurrentDomain.FriendlyName));
    } else {
      foreach(string arg in args) {
        Console.WriteLine("Hello {0}", arg);
      }
    }
  }
}

This post - or rather the code for it - was contributed by IISResetMe.

polyglotbitscsharpread-program-arguments