C# Main args Examples

Use the args parameter in the Main entry point method. Handle a string array in Main.

dot net perls

Main, args. A 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.

Benefits, arguments. With arguments it is possible to configure programs with minimal complexity. Sometimes no external configuration files are even necessary.Array

First example. 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.Void
Also: You can use a Main method that returns an int or that receives no parameters if you want to. Main() can be defined in any class.
ReturnInt, uint
Next: This program shows how the command-line parameters are received from a Windows command line.
Step 1: Upon startup the Main method is executed. It then tests for a null array argument.
Step 2: We print the Length of the arguments, and then loop over them. We write the arguments with Console.WriteLine.
C# program that receives command-line arguments 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("]"); } } Console.ReadLine(); } } Output "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 ]

Whitespace. 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.
But: If you want to preserve whitespace on the command line, use the quotation marks around your parameter.
Equivalent command lines: "C:\ConsoleApplication1.exe" a b c "C:\ConsoleApplication1.exe" a b c

Signature. You can change the signature of the Main method in your program and the program will still compile and execute correctly.
Return int: You can have the Main method return an int. This tells the operating system the program's result.
Tip: It is also useful to omit the string[] args parameter array entirely when not needed.
C# program that returns int from Main class Program { static int Main() { // We can return an int from a program exe. System.Console.WriteLine("[DONE]"); return 600; } } Output [DONE]

Foreach, for. 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: The foreach-loop is a good choice when the index of each element is not important to its usage.
For: When we need to do something special with element 1 or element 3, the index is important, so it is better to use a for-loop.
C# program that uses args loops 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); } Console.ReadLine(); } } Output foreach: Arg1 foreach: Arg2 foreach: Arg3 for: Arg1 for: Arg2 for: Arg3

Shortcut. 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.
Then: Right-click on the EXE file and select Create Shortcut. Find the "Target" text box in the Shortcut Properties window.
Finally: Append the command-line arguments after the file name in the Target text box.
Result: When you click on the shortcut, these strings will be put into the string[] args array at runtime in the Main method.
Arguments from screenshot: a b

Null. 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.Null
However: If incorrect parameters are likely, it is best to wrap Main() with try, catch and finally blocks.
Tip: This ensures the best possible execution paths are taken. If a step is critical, put it in a finally block.

A summary. 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.


© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.