Search This Blog

Tuesday, March 4, 2014

How to pass command line argument in C# Program

As we know "Main()" is the entry point of our C# Program there is provision for pass 
command line argument as void Main(string[] args) this indicates that while we run
the program we could pass the string array this acts like the runtime parameter and this
array is used as a simple array.
for example , if I run the program as 
mycmd A B C D 
the output will be

Number of command line parameters = 3
Arg[0] = [A]
Arg[1] = [B]
Arg[2] = [C]


// mycmd.cs
// arguments: A B C
using System;

public class AnyClass
{
   public static void Main(string[] args)
   {
       // The Length property is used to obtain the length of the array. 
       // Notice that Length is a read-only property:
       Console.WriteLine("Number of command line parameters = {0}",
          args.Length);
       for(int i = 0; i < args.Length; i++)
       {
           Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
       }
   }
}

No comments:

Post a Comment