Main
, argsA C# program has a Main
entry point. In Main
, we access a string
array called "args." This array is populated with command-line arguments from the operating system.
With arguments it is possible to configure programs with minimal complexity. Sometimes no external configuration files are even necessary.
When you create a new console application in the C# language using Visual Studio, you will get a Main
method. It will have a string
args parameter and void
return type.
Main
method that returns an int
or that receives no parameters if you want to. Main()
can be defined in any class
.Main
method is executed. It then tests for a null
array argument.Length
of the arguments, and then loop over them. We write the arguments with Console.WriteLine
.using System; class Program { static void Main(string[] args) { // Step 1: test for null. if (args == null) { Console.WriteLine("args is null"); } else { // Step 2: print length, and loop over all arguments. Console.Write("args length is "); Console.WriteLine(args.Length); for (int i = 0; i < args.Length; i++) { string argument = args[i]; Console.Write("args index "); Console.Write(i); // Write index Console.Write(" is ["); Console.Write(argument); // Write string Console.WriteLine("]"); } } } }"C:\ConsoleApplication1.exe" a b c args length is 3 args index 0 is [a] args index 1 is [b] args index 2 is [c] "C:\ConsoleApplication1.exe" a b c args length is 3 args index 0 is [a] args index 1 is [b] args index 2 is [c] "C:\\ConsoleApplication1.exe" http://www.dotnetperls.com/ args length is 1 args index 0 is [http://www.dotnetperls.com/] "C:\ConsoleApplication1.exe" "Literal test " args length is 1 args index 0 is [Literal test ]
In Windows, you can open your program's Release or Debug directory and create a shortcut to your application and specify command-line parameters in that shortcut.
Find
the "Target" text box in the Shortcut Properties window.Append
the command-line arguments after the file name in the Target text box.string
args array at runtime in the Main
method.a b
More than one whitespace is discarded when separating the arguments. So if you separate parameters by two spaces, there is no difference from separating with one space.
"C:\ConsoleApplication1.exe" a b c "C:\ConsoleApplication1.exe" a b c
You can change the signature of the Main
method in your program and the program will still compile and execute correctly.
Main
method return an int
. This tells the operating system the program's result.string
args parameter array entirely when not needed.class Program { static int Main() { // We can return an int from a program exe. System.Console.WriteLine("[DONE]"); return 600; } }[DONE]
Compile this program and then create a shortcut to it. Add some arguments to the shortcut target. When you execute this program, it will loop through all these arguments.
foreach
-loop is a good choice when the index of each element is not important to its usage.for
-loop.using System; class Program { static void Main(string[] args) { // The program control flow begins here. foreach (string value in args) { Console.WriteLine("foreach: {0}", value); } for (int i = 0; i < args.Length; i++) { string value = args[i]; Console.WriteLine("for: {0}", value); } } }foreach: Arg1 foreach: Arg2 foreach: Arg3 for: Arg1 for: Arg2 for: Arg3
In my testing, you will not encounter a null
parameter in Main
. So if you execute a program with no command-line parameters, you will not receive a null
array.
Main()
with try, catch and finally blocks.We saw the exact behavior of the Main
entry point when used with an args string
array. We invoked Main()
from the Windows operating system with shortcuts.