Command Line Arguments

Recall that the Main() method is special in that it is automatically invoked by the .NET runtime when program execution commences. It holds another special feature—it is possible to enter data (here called arguments) into the Main() method from the console window (or a Windows application) when the program initially is started. To enable this functionality, we need to include a formal parameter of type reference-to-array-object of base type string in the method header of Main() as in the following:

public static void Main(string [] args)

where the array variable, as per tradition, is called args.

Passing arguments from the console into Main() can now be done simply by typing these next to the text you usually type (typically ...

Get C# Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.