C# command line arguments

In C#, it is possible to pass command line arguments to a program when executing it. Command line arguments are additional parameters provided to a program when it is run from the command line or terminal.

When a C# program is executed with command line arguments, these arguments are passed to the program as an array of strings called args. The args array holds the command line arguments in the order they were provided.

For example, if you run a C# program with the following command line:

dotnet run arg1 arg2 arg3

The program will receive the command line arguments as follows:

args[0] will be "arg1" args[1] will be "arg2" args[2] will be "arg3"

You can access and utilize these command line arguments within your C# program to modify its behavior or process specific data based on the input provided.

Command line arguments

Here's a simple example demonstrating how to access and use command line arguments in a C# program:

Open a new text document and copy and paste the following source code and save the file as "NewProg.cs"

Full Source C#
using System; class NewProg { static void Main(string[] args) { Console.WriteLine("Arguments-1 " + args[0]+" Argument-2 "+args[1]); Console.ReadKey(); } }

Go to the command prompt and issue the following command for compilation.

csc NewProg.cs

After the successful compilation you will get NewProg.exe file

When you execute this C# program you have to pass two arguments with the filename.

NewProg test1 test2

you will get the output like Arguments-1 test1 Argument-2 test2

Conclusion

Command line arguments provide a way to pass input or configuration options to your C# program from the command line, allowing for greater flexibility and customization.