Command Line arguments in C#


If you want to pass arguments by command line, then use command line arguments in C# −

When we create a program in c#, static void main is used and we can see the arguments in it .

class HelloWorld {
   static void Main(string[] args) {
      /* my first program in C# */
      Console.WriteLine("Hello World");
      Console.ReadKey();
   }

The string[] args is a variable that has all the values passed from the command line as shown above.

Now to print those arguments, let’s say we have an argument, “One” −

Console.WriteLine("Length of the arguments: "+args.Length);
Console.WriteLine("Arguments:");
foreach (Object obj in args) {
   Console.WriteLine(obj);
}

The above will print −

Length of the arguments: 1
Arguments: One

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements