Home
C#
Main args, Command-Line Arguments
This page was last reviewed on Nov 25, 2023.
Dot Net Perls
Main, args. A 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.
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.
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.
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 ]
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.
a b
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.
"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.
Detail 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.
class Program { static int Main() { // We can return an int from a program exe. System.Console.WriteLine("[DONE]"); return 600; } }
[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.
Tip The foreach-loop is a good choice when the index of each element is not important to its usage.
foreach
However 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.
for
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
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.
try
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 25, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.