C# command line arguments
last modified October 5, 2026
In this article we show how to work with command line arguments in C#.
Command line arguments are values passed to console programs, usually through the terminal.
We can send command line arguments to a Main method by declaring
a parameter with the string[] type. Programs that use top-level
statements have a built-in args variable that contains the
arguments.
C# command line arguments in Main
In the first example, we pass command line arguments to a Main
method.
class Program
{
static void Main(string[] args)
{
foreach (var arg in args)
{
Console.WriteLine(arg);
}
}
}
In this program, we print all arguments to the console.
static void Main(string[] args)
Command line arguments have the string[] type. It is a convention
to name the variable args. The array does not include the program
name; its first element is the first user-supplied argument.
$ dotnet run John Doe gardener John Doe gardener
C# command line arguments with top-level statements
When a program uses top-level statements and has no explicit Main
method, the arguments are accessible via the built-in args
variable.
Console.WriteLine($"You have passed {args.Length} arguments");
foreach (var arg in args)
{
Console.WriteLine(arg);
}
The program prints the number of command line arguments and then prints each argument.
$ dotnet run 1 2 3 4 5 6 You have passed 6 arguments 1 2 3 4 5 6
C# Environment.GetCommandLineArgs
Environment.GetCommandLineArgs() returns a string array containing
the command-line arguments for the current process.
string[] cargs = Environment.GetCommandLineArgs();
Console.WriteLine($"You have passed {cargs.Length} arguments");
foreach (var arg in cargs)
{
Console.WriteLine(arg);
}
It is more explicit than the built-in args variable. Also,
Environment.GetCommandLineArgs() includes the executable path as
the first element of the array.
$ dotnet run 1 2 3 4 5 You have passed 6 arguments /home/jano/Documents/prog/csharp/cmd-args/SimpleEx/bin/Debug/net10.0/SimpleEx.dll 1 2 3 4 5
C# Environment.CommandLine
Environment.CommandLine returns a string containing the command
line used to start the current process.
string cargs = Environment.CommandLine;
Console.WriteLine(cargs);
string[] vals = cargs.Split(" ");
try
{
int sum = vals.Skip(1).Select(e => Convert.ToInt32(e)).Sum();
Console.WriteLine($"The sum of values is {sum}");
return 0;
}
catch (FormatException e)
{
Console.WriteLine("Invalid input");
Console.WriteLine(e.Message);
return 1;
}
The program expects a list of integers as input.
string cargs = Environment.CommandLine;
We get the command line input with Environment.CommandLine.
string[] vals = cargs.Split(" ");
We split the string into parts.
int sum = vals.Skip(1).Select(e => Convert.ToInt32(e)).Sum();
We skip the first element, which is the executable path. We convert the strings into integers and compute a sum.
catch (FormatException e)
{
Parsing command line arguments is error-prone. A FormatException
is thrown when a value cannot be converted to an integer.
$ dotnet run 1 2 3 4 5 The sum of values is 15
C# System.CommandLine
System.CommandLine is the modern command line parsing library developed by Microsoft for .NET applications.
$ dotnet add package System.CommandLine
We add the System.CommandLine package.
using System.CommandLine;
var valsOption = new Option<int[]>("--vals")
{
Description = "the values to sum",
AllowMultipleArgumentsPerToken = true
};
var rootCommand = new RootCommand("Sums integer values passed on the command line");
rootCommand.Options.Add(valsOption);
rootCommand.SetAction(parseResult =>
{
int[] vals = parseResult.GetValue(valsOption) ?? [];
Console.WriteLine($"The sum is {vals.Sum()}");
});
return rootCommand.Parse(args).Invoke();
The program defines a --vals option that accepts a sequence of
integers and prints their sum.
$ dotnet run --vals 1 2 3 4 5 6 The sum is 21
Source
Main and command-line arguments
In this article we have worked with command line arguments in C#.
Author
List all C# tutorials.