Arguments passed to a console app arrive as a string[] named args. With top-level statements (the default since .NET 6), args is available without declaring it:
Console.WriteLine($"{args.Length} argument(s)");
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine($"args[{i}] = {args[i]}");
}
Run it with dotnet run. Everything after -- is passed to your program instead of being read by dotnet itself:
dotnet run -- input.txt --verbose "two words"
Output:
3 argument(s)
args[0] = input.txt
args[1] = --verbose
args[2] = two words
The shell splits the command line into arguments before your program sees it. Quotes group words into one argument and are removed, so "two words" arrives as a single string without quotes. The exact quoting rules belong to the shell: bash, PowerShell and cmd.exe differ in how they treat single quotes, $ and backslashes.
In a project with a classic entry point, args is the parameter of Main:
static void Main(string[] args)
{
foreach (string argument in args)
{
Console.WriteLine(argument);
}
}
In both cases, args contains only the arguments. The program name is not included, and with no arguments args is an empty array, never null.
Environment.GetCommandLineArgs
The original version of this example used Environment.GetCommandLineArgs(). It returns the same arguments with one extra element at the front: the path of the program itself. That is useful in code that is not the entry point, such as a library or a logging setup, because it does not need args passed down:
string[] all = Environment.GetCommandLineArgs();
Console.WriteLine($"Program: {all[0]}");
foreach (string argument in all.Skip(1))
{
Console.WriteLine(argument);
}
Forgetting to skip that first element is the classic mistake with this method. For a framework-dependent app started with dotnet, the first element is the path of the .dll, not of dotnet itself.
Parse simple options
For a small tool, a loop with a switch is enough:
bool verbose = false;
string? output = null;
var files = new List<string>();
for (int i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "-v" or "--verbose":
verbose = true;
break;
case "-o" or "--output":
if (i + 1 >= args.Length)
{
Console.Error.WriteLine("Missing value for --output");
return 1;
}
output = args[++i];
break;
default:
files.Add(args[i]);
break;
}
}
Console.WriteLine($"verbose={verbose}, output={output ?? "(none)"}, files={files.Count}");
return 0;
Running it with dotnet run -- -v a.txt b.txt -o result.txt prints verbose=True, output=result.txt, files=2.
Returning an int from top-level statements (or from Main) sets the process exit code. Return 0 for success and a non-zero value for errors, so scripts and CI pipelines can detect the failure. Write error messages to Console.Error, not Console.Out.
Once you need help text, typed values, required options or subcommands, stop hand-rolling and use a library. Microsoft’s System.CommandLine package handles parsing, validation, --help and tab completion. If the app already uses Microsoft.Extensions.Configuration (as ASP.NET Core and the Generic Host do), command-line arguments such as --Logging:LogLevel:Default=Debug are read into configuration automatically and override appsettings.json.
Set arguments in Visual Studio
To debug with arguments, open the project’s properties, go to Debug, then Open debug launch profiles UI, and fill in Command line arguments. Visual Studio stores them in Properties/launchSettings.json as "commandLineArgs", which dotnet run also reads. In VS Code, put them in the args array of the launch configuration in .vscode/launch.json.
Do not pass secrets such as passwords or API keys as arguments. On most systems, other users on the same machine can see the full command line of running processes. Use environment variables or a secrets store instead.