The Main( ) Method


Up to this point, you have been using one form of Main( ). However, there are several overloaded forms of Main( ). Some can be used to return a value, and some can receive arguments. Each is examined here.

Returning Values from Main( )

When a program ends, you can return a value to the calling process (often the operating system) by returning a value from Main( ). To do so, you can use this form of Main( ):

 static int Main( )

Notice that instead of being declared void, this version of Main( ) has a return type of int.

Usually, the return value from Main( ) indicates whether the program ended normally or due to some abnormal condition. By convention, a return value of 0 usually indicates normal termination. All other values indicate some type of error occurred.

Passing Arguments to Main( )

Many programs accept what are called command-line arguments. A command-line argument is the information that directly follows the program’s name on the command line when it is executed. For C# programs, these arguments are then passed to the Main( ) method. To receive the arguments, you must use one of these forms of Main( ):

 static void Main(string[ ] args) static int Main(string[ ] args)

The first form returns void; the second can be used to return an integer value, as described in the preceding section. For both, the command-line arguments are stored as strings in the string array passed to Main( ).

For example, the following program displays all of the command-line arguments that it is called with:

 // Display all command-line information. using System; class CLDemo {   public static void Main(string[] args) {     Console.WriteLine("There are " + args.Length +                        " command-line arguments.");     Console.WriteLine("They are: ");     for(int i=0; i < args.Length; i++)       Console.WriteLine(args[i]);   } }

If CLDemo is executed like this:

 CLDemo one two three

you will see the following output:

 There are 3 command-line arguments. They are: one two three

To understand the way that command-line arguments can be used, consider the next program. It encodes or decodes messages. The message to be encoded or decoded is specified on the command line. The encryption method is very simple: to encode a word, each letter is incremented by 1. Thus, A becomes B, and so on. To decode, each letter is decremented.

 // Encode or decode a message. using System; class Cipher {   public static int Main(string[] args) {     // see if arguments are present     if(args.Length < 2) {       Console.WriteLine("Usage: encode/decode word1 [word2...wordN]");       return 1; // return failure code     }     // if args present, first arg must be encode or decode     if(args[0] != "encode" & args[0] != "decode") {       Console.WriteLine("First arg must be encode or decode.");       return 1; // return failure code     }     // encode or decode message     for(int n=1; n < args.Length; n++) {       for(int i=0; i < args[n].Length; i++) {         if(args[0] == "encode")           Console.Write((char) (args[n][i] + 1) );         else           Console.Write((char) (args[n][i] - 1) );       }       Console.Write(" ");     }     Console.WriteLine();     return 0;   } }

To use the program, specify either the “encode” or “decode” command followed by the phrase that you want to encrypt or decrypt. Assuming the program is called Cipher, here are two sample runs:

 C:>Cipher encode one two pof uxp C:>Cipher decode pof uxp one two

There are two interesting things in this program. First, notice how the program checks that a command-line argument is present before it continues execution. This is very important and can be generalized. When a program relies on there being one or more command-line arguments, it must always confirm that the proper arguments have been supplied. Failure to do this can lead to program malfunctions. Also, since the first command-line argument must be either “encode” or “decode”, the program also checks this before proceeding.

Second, notice how the program returns a termination code. If the required command line is not present, then 1 is returned, indicating abnormal termination. Otherwise, 0 is returned when the program ends.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net