Thursday, June 20, 2013

Command Line Arguments

Command line arguments are parameters supplied to the Main method at the time of invoking it for execution. To understand the concepts see the example given below:

Program of Command line argument to accept name from the command line and writes to the console.

using System;
class sample
{
       public static void Main( string[ ] args)
  {
       console.write("welcome to");
       console.write(" " +args[0]);
       console.write(" " +args[l]);
  }
}

Execution for this program
C:\ > Sample My Home
Output of Example
Welcome to My Home

In Previous examples, we have used the Main method with no parameters. Notice that in the above example how the Main is declared.
Main is declared with a parameters args. The parameter args is an array of strings. Any arguments provided in the command line at the time of execution, are passed to the array args as its elements. We can access the array elements by using a subscript like args[0], args[1] and so on.

No comments:

Post a Comment