Command Line
Argument:
It
is possible to pass some values from the command line to your C# programs when
they are executed. The arguments can be used as input for the program. A
parameter supplied to the program through the command line is when it invoked
is called Command Line Argument. We can pass any number of arguments from the
command prompt. The Main() method is entry point method for a program. Command
line arguments are passed to the Main() method.
In
C# Main() method can be declared with the String[] args parameter. The args
parameter accepts command-line arguments which can be used to provide the
program some values at the moment of execution. The args argument is array of
type String. The program accept arguments in the order of args[0], args[1] and
so on. The arguments are separated by white space, i.e. either space or tab.
For Example:
using
System;
class
Sample
{
static void Main(string[] args)
{
int SUM = 0;
int X = 0;
int Y = 0;
X =
Convert.ToInt32(args[0]); // Command Line Argument 1
Y =
Convert.ToInt32(args[1]); // Command
Line Argument 2
SUM = X + Y;
Console.WriteLine("Sum
is: "+SUM);
}
}
To Compile:
csc sample.cs
Here, we are passing 10 and 20 as command line arguments which can be
accessed through args. Where 10 will be assigned into args[0] and 20 will be
assigned into args[1].
No comments:
Post a Comment