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 just the name of the program) to invoke the program. For example, to pass the arguments

Planet Blipos is far away

into the Main() method of the program in Listing 10.10 called ConsoleArguments.cs, you must write the following commands in the console window, after having created the ConsoleArguments.cs file with the editor. First, the program is compiled as usual:

 C:\MyC#Programs>csc ConsoleArguments.cs<enter> 

Then, the arguments are typed next to the name of the program as follows:

 C:\MyC#Programs>ConsoleArguments Planet Blipos is far away<enter> 

In this case, our program simply prints the content of the args array variable back to the screen, resulting in the sample output shown after the listing.

Listing 10.10 ConsoleArguments.cs
01: using System; 02: 03: class ConsoleArguments 04: { 05:     public static void Main(String [] args) 06:     { 07:         for (int i = 0; i < args.Length; i++) 08:         { 09:             Console.WriteLine(args[i]); 10:         } 11:     } 12: } Planet Blipos is far away 

The text entered after the name of the program (in this case, Planet Blipos is far away) is analyzed by the .NET runtime and automatically divided into separate words. A word is defined to have whitespace or begin/end-of-text on either side. Every word is then assigned to an array element of args in the order in which it was written. Thus, immediately after the method body of Main() has begun executing (line 7), the array object referenced by args has the following content, which is verified by the sample output generated by lines 7 10.

args[0] args[1] args[2] args[3] args[4]
Planet Blipos is far away



args of

 public static void Main(String [] args) 

is merely an identifier. Consequently, you can use any other name for this parameter, providing that you follow the usual rules for variable naming (avoid using keywords and so on).

It is possible to enter several words into the same array element by using quotation marks. For example, the following command

 C:\MyC#Programs>ConsoleArguments "Wolfgang Amadeus Mozart" was  a composer <enter> 

results in the following output:

 Wolfgang Amadeus Mozart was a composer 

obviously assigning the string "Wolfgang Amadeus Mozart" to the first array element, despite the whitespace between Wolfgang, Amadeus, and Mozart.

Sometimes, the user might want to pass numbers to a program. This can be achieved by letting the program convert the relevant array elements of args from type string to the suitable numerical type. For example, the program of Listing 10.11 reads in two numbers and prints out their sum.

Listing 10.11 SumOfArguments.cs
01: using System; 02: 03: class SumOfArguments 04: { 05:     public static void Main(string[] args) 06:     { 07:         int x; 08:         int y; 09: 10:         x = Convert.ToInt32(args[0]); 11:         y = Convert.ToInt32(args[1]); 12:         Console.WriteLine( 13:             "The sum of the two arguments is: { 0} ", (x+y)); 14:     } 15: } C:\MyC#Programs>SumOfArguments 100 200<enter> The sum of the two arguments is: 300 

The two numeric arguments (100 and 200 in this example) reside inside the first and second array elements in the form of strings. To involve the numbers in any calculations, they must first be converted to an appropriate numeric type, in this case int. The conversions are performed in lines 10 and 11 and assigned to the two int variables x and y. Line 13 prints out the sum of x and y.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: