C Sharp Command Line Arguments

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

C# Command Line Arguments

Arguments that are passed by command line known as command line arguments. We can send arguments to the Main method while executing the code. The string args variable contains all the values passed from the command line.

In the following example, we are passing command line arguments during execution of program.

C# Command Line Arguments Example

  1. using System;  
  2. namespace CSharpProgram  
  3. {  
  4.     class Program  
  5.     {  
  6.         // Main function, execution entry point of the program  
  7.         static void Main(string[] args) // string type parameters  
  8.         {  
  9.             // Command line arguments  
  10.             Console.WriteLine("Argument length: "+args.Length);  
  11.             Console.WriteLine("Supplied Arguments are:");  
  12.             foreach (Object obj in args)  
  13.             {  
  14.                 Console.WriteLine(obj);       
  15.             }  
  16.         }  
  17.     }  
  18. }  

Compile and execute this program by using following commands.

Compile: csc Program.cs

Execute: Program.exe Hi there, how are you?

After executing the code, it produces the following output to the console.


Argument length: 5
Supplied Arguments are: