C# Main, Args

Main method

Every 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. This makes it possible to configure programs with minimal complexity.

String Array

Example

C# programming language

When you create a new console application in the C# language using Visual Studio, you will get a Main method with a signature similar to the one shown here. It will have a string[] args parameter and void return type.

ArgsVoid

Also:You can use a Main method that returns an int or that receives no parameters if you want to.

ReturnInt

Next:This program shows how the command-line parameters are received from a Windows command line.

C# program that receives command-line arguments

using System;

class Program
{
    static void Main(string[] args)
    {
	if (args == null)
	{
	    Console.WriteLine("args is null"); // Check for null array
	}
	else
	{
	    Console.Write("args length is ");
	    Console.WriteLine(args.Length); // Write array length
	    for (int i = 0; i < args.Length; i++) // Loop through array
	    {
		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 ]
Class: shapes

Main can be defined in any valid class name. Note that you must have a class enclosing all methods in your C# program. Upon startup the Main method is executed. It then tests for a null array argument—this is never reached.

Class

Then:It prints the array length and then writes the arguments as strings with Console.WriteLine.

Console.WriteLine

Note:The "Output" section of the example shows possible command lines and the program's actual output.

Whitespace

This section provides information

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 with the string literal syntax, enclosed whitespace is retained.

Tip:If you want to preserve whitespace on the command line, use the quotation marks around your parameter.

Null keyword

Null. In my testing, you will not encounter a null parameter array in the Main entry point. So if you execute a program with no command-line parameters, you will not receive a null string array reference in the Main method.

Null

However, if incorrect parameters are likely, it is best to wrap the Main entry point with try, catch and finally blocks. This ensures the best possible execution paths are taken. If a step is critical, put it in a finally block.

TryCatchFinally

Signature

Programming tip

You can change the signature of the Main method in your program and the program will still compile and execute correctly. You can demand that 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.

Also:This site almost always omits the parameter array to shorten the example programs and improve clarity.

Shortcut

Framework: NET

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. Right-click on the EXE file and select Create Shortcut.

Then, in the "Target" text box in the Shortcut Properties window, append the command-line arguments after the file name. When you click on the shortcut, these strings will be put into the string[] args array at runtime in the Main method.

Summary

We saw the exact behavior of the Main entry point when used with an args string array. We saw examples of calling the Main method from the Windows operating system with shortcuts. We noted how whitespace in the command-line is handled.


C#: .NET: Console