Command Line Arguments

C# Command Line Arguments

When an argument is passed by the command line, then it is called as a command-line argument. The arguments are passed to the Main method during the execution of the code. The values to be passed from the command line, are specified in the string args variable.

Example:

using System;  
namespace Commandlineexample  
{  
    class Example  
    {  
        // Main function, execution entry point of the program  
        static void Main(string[] args) 
        {  
            // Command line arguments  
            Console.WriteLine("Length of the argument: "+args.Length);  
            Console.WriteLine("Passed Arguments are:");  
            foreach (Object x in args)  
            {  
                Console.WriteLine(x);       
            }  
        }  
    }  
}

Output:

Length of the argument: 2

Passed Arguments are:

Hello

World!!

Explanation:

In the above example, the command-line arguments are being passed during the execution of the program. For compilation and execution of the code, the below commands can be used and the output will be produced to the console.

Compilation: csc Example.cs

Execution: Example.exe Hello World!!