Command-line parameters allow users to provide options or instructions when starting a C# application. In .NET, these parameters are available through the args array passed to the Main method.
One simple way to check for a specific command-line parameter is to use args.Contains().
What is args?
When a C# application starts, the Main method can receive a string[] args parameter:
static void Main(string[] args)
{
// Command-line arguments are available in args
}Each item in args represents one argument provided when launching the application.
For example, if the application is started with:
MyProgram.exe --verbose --help
the args array contains:
args[0] = "--verbose"
args[1] = "--help"
Using args.Contains()
The Contains() method can be used to check whether a particular argument was provided.
static void Main(string[] args)
{
if (args.Contains("--verbose"))
{
Console.WriteLine("Verbose mode enabled.");
}
if (args.Contains("--help"))
{
Console.WriteLine("Displaying help...");
}
}
If the application is started with:
MyProgram.exe --verbose
then:
args.Contains("--verbose")
returns true.
If --verbose was not provided, it returns false.
A Practical Example
Suppose we have a program that supports three options:
--verbose— enables detailed output--help— displays help information--version— displays the application version
We can check each option like this:
static void Main(string[] args)
{
if (args.Contains("--help"))
{
Console.WriteLine("Usage: MyProgram [options]");
Console.WriteLine("--verbose Enable verbose output");
Console.WriteLine("--version Show application version");
}
if (args.Contains("--version"))
{
Console.WriteLine("MyProgram version 1.0");
}
if (args.Contains("--verbose"))
{
Console.WriteLine("Verbose mode enabled.");
}
}
The user could then run:
MyProgram.exe --verbose --version
The program would recognize both parameters because both values are present in the args array.
Why Use Contains()?
For simple command-line switches, args.Contains() is convenient because it makes the code easy to read:
if (args.Contains("--verbose"))
{
// Enable verbose mode
}
It is particularly useful when an argument represents a simple on/off option, such as --help, --debug, or --verbose.
Parameters With Values
Contains() is best suited to flags that do not require a value.
For example:
MyProgram.exe --verbose
works well with:
args.Contains("--verbose")
However, a parameter such as:
MyProgram.exe --name John
is different. --name has an associated value, John. In that situation, you need additional logic to retrieve the value.
For simple applications, you might inspect the argument following --name:
int index = Array.IndexOf(args, "--name");
if (index >= 0 && index + 1 < args.Length)
{
string name = args[index + 1];
Console.WriteLine($"Hello, {name}!");
}
Conclusion
The args array provides a straightforward way to access command-line arguments in C#. For simple flags, args.Contains() is an easy and readable solution:
if (args.Contains("--verbose"))
{
// Do something
}
It is a good approach when an application only needs a small number of simple command-line switches. For applications with many parameters, values, validation, or more complex command-line syntax, a dedicated command-line parsing library or framework can provide a more robust solution.