by Dinesh Thakur Category: Introduction to C#

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. (Sample.cs)

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.

Like as in the command line" C:\> Sample My Home", args[O] and args[1] contains:

Args [0] <-- My

args [1] <-- Home

There fore the values will be display of these parameters in Console.Write() statement.



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.