C# Command Line Arguments

Command-line arguments are values supplied when a C# application is started from a terminal, command prompt, script, or process launcher. A program can use these values to select a mode, read a file name, set an option, or receive other input without prompting the user interactively.

For example, an application may accept an argument such as test or production to choose its operating mode. A file-processing program may accept a path, while a utility may accept flags such as --verbose.

The Main() method can receive command-line arguments through a string[] parameter. Each item supplied after the program command becomes one element of the array.

C# Main Method Syntax for Command-Line Arguments

A common entry-point declaration uses a parameter named args:

</>
Copy
static void Main(string[] args)
{
    // Access command-line arguments through args
}

The name args is conventional, but it is not required. Any valid parameter name can be used. The first argument is stored at index 0, the second at index 1, and so on. The executable name itself is not included in this array.

Example – Command Line Arguments in C#

In the following example, we have Main() function with string[] as arguments in its definition. We shall print the number of arguments received and the arguments as well.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Number of Arguments : " + args.Length);  
            Console.WriteLine("Arguments provided in the command line are: ");  
            foreach (string str in args) {  
                Console.WriteLine(str);       
            }  
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run TutorialKart C#
Number of Arguments : 2
Arguments provided in the command line are:
TutorialKart
C#

After the command dotnet run, we have provided two strings TutorialKart and C# as arguments. Hence the number of arguments printed is 2.

The array contents in this run are:

  • args[0] contains TutorialKart.
  • args[1] contains C#.
  • args.Length is 2.

Passing Arguments with dotnet run

For a simple project, arguments may be written after dotnet run. However, the dotnet command also has options of its own. A double dash -- clearly separates options intended for dotnet run from arguments intended for the application.

</>
Copy
dotnet run -- first second

With this command, the application receives first and second. The separator itself is not included in args.

When running an already built application, arguments are placed after the DLL or executable name:

</>
Copy
dotnet MyApplication.dll first second

Passing a C# Command-Line Argument That Contains Spaces

Use quotation marks when one argument contains spaces. Without quotation marks, the shell normally splits the text into multiple arguments.

</>
Copy
dotnet run -- "TutorialKart C#" beginner

The application receives two arguments:

args[0] = TutorialKart C#
args[1] = beginner

Quotation rules and escape characters can differ between PowerShell, Command Prompt, Bash, and other shells. The shell processes the command first and then passes the resulting strings to the C# program.

Reading a Command-Line Argument by Index

Check args.Length before reading an argument by index. Accessing an index that does not exist causes an IndexOutOfRangeException.

</>
Copy
using System;

class Program
{
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Please provide a name.");
            return;
        }

        Console.WriteLine("Hello " + args[0]);
    }
}

Running the program with a name:

</>
Copy
dotnet run -- Alice

produces:

Hello Alice

Converting C# Command-Line Arguments to Numbers

Every command-line argument arrives as a string. Convert an argument before using it as an integer, decimal, Boolean, date, or another data type. For user-supplied input, TryParse is generally safer than Parse because it handles invalid values without throwing a format exception.

</>
Copy
using System;

class Program
{
    static void Main(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine("Usage: app <first-number> <second-number>");
            return;
        }

        if (!int.TryParse(args[0], out int first) ||
            !int.TryParse(args[1], out int second))
        {
            Console.WriteLine("Both arguments must be valid integers.");
            return;
        }

        Console.WriteLine("Sum: " + (first + second));
    }
}

Run the program as follows:

</>
Copy
dotnet run -- 12 8

Output

Sum: 20

Using Named Options in C# Command-Line Arguments

A program may interpret values beginning with -- as named options. The following example checks for a --verbose flag and reads a value following --mode.

</>
Copy
using System;

class Program
{
    static void Main(string[] args)
    {
        bool verbose = false;
        string mode = "production";

        for (int i = 0; i < args.Length; i++)
        {
            if (args[i] == "--verbose")
            {
                verbose = true;
            }
            else if (args[i] == "--mode" && i + 1 < args.Length)
            {
                mode = args[++i];
            }
        }

        Console.WriteLine("Mode: " + mode);
        Console.WriteLine("Verbose: " + verbose);
    }
}
</>
Copy
dotnet run -- --mode test --verbose

Output

Mode: test
Verbose: True

This manual approach is suitable for small examples. Applications with many commands, aliases, required options, help text, and validation rules usually benefit from a dedicated command-line parsing library.

Example – Program that does not consider command line arguments

You can define your Main() function with no string[] as argument. In this case, even if you provide command line arguments, those are not captured as arguments to the Main() function.

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main() {
            Console.WriteLine("The program does not consider any arguments");  
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
The program does not consider any arguments
PS D:\workspace\csharp\HelloWorld> dotnet run TutorialKart
The program does not consider any arguments

The operating system or .NET host may still receive the supplied values, but a parameterless Main() method does not expose them through an argument array.

Alternative Main Method Return Types

The C# entry point can return void, int, Task, or Task<int>, with or without a string[] parameter. Returning an integer allows the program to report an exit code to the calling process.

</>
Copy
using System;

class Program
{
    static int Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.Error.WriteLine("A file name is required.");
            return 1;
        }

        Console.WriteLine("Processing: " + args[0]);
        return 0;
    }
}

By convention, an exit code of 0 indicates success, while a nonzero value indicates an error or another defined result.

C# Command-Line Arguments with Top-Level Statements

Modern C# projects can use top-level statements without explicitly declaring a Program class or Main() method. In such a file, the implicit args variable contains the command-line arguments.

</>
Copy
Console.WriteLine("Argument count: " + args.Length);

foreach (string argument in args)
{
    Console.WriteLine(argument);
}

This is equivalent in purpose to receiving string[] args in an explicitly declared Main() method.

C# Command-Line Argument Validation Checklist

  • Check args.Length before accessing an index.
  • Use quotation marks when an argument contains spaces.
  • Use TryParse when converting user input to numeric or other structured types.
  • Reject unknown options instead of silently ignoring likely typing errors.
  • Display a clear usage message when required arguments are missing.
  • Do not place passwords, access tokens, or other secrets directly on the command line because they may be visible in shell history or process listings.
  • Return a meaningful nonzero exit code when the command cannot complete successfully.

C# Command-Line Arguments: Common Questions

Does args[0] contain the C# executable name?

No. In a C# Main(string[] args) method, args[0] contains the first argument supplied to the application. The executable or DLL name is not included.

What happens when no command-line arguments are supplied?

The args array is empty and args.Length is 0. Code should verify the length before accessing args[0].

Why are all C# command-line arguments strings?

The shell passes command-line values as text. The program must parse each value into the required type, such as int, double, bool, or DateTime.

How do I pass a file path containing spaces?

Enclose the complete path in quotation marks, such as "C:\My Files\input.txt". The exact quoting and escaping rules depend on the shell being used.

Can a C# program ignore supplied command-line arguments?

Yes. A program can declare a parameterless Main() method or simply choose not to read the args array.

C# Command-Line Arguments Summary

C# applications receive command-line arguments as strings through Main(string[] args) or the implicit args variable in a top-level program. Use args.Length to validate the input, read values by index, quote arguments containing spaces, and parse strings safely before using them as typed values.

In this C# Tutorial, we learned about Command Line Arguments in C#, how to provide these arguments to the program, how to access these values with examples.