Returns and Parameters on Main()


Returns and Parameters on Main()

So far, declaration of an executable's Main() method has been the simplest declaration possible. You have not included any parameters or return types in your Main() method declarations. However, C# supports the ability to retrieve the command-line arguments when executing a program, and it is possible to return a status indicator from the Main() method.

The runtime passes the command-line arguments to Main() using a single string array parameter. All you need to do to retrieve the parameters is to access the array, as demonstrated in Listing 4.10. The purpose of this program is to download a file whose location is given by a URL. The first command-line argument identifies the URL, and the optional second argument is the filename to which to save the file. The listing begins with a switch statement that evaluates the number of parameters (args.Length) as follows.

  1. If there are zero parameters, display an error indicating that it is necessary to provide the URL.

  2. If there is only one argument, calculate the second argument from the first argument.

  3. The presence of two arguments indicates the user has provided both the URL of the resource and the download target filename.

Listing 4.10. Passing Command-Line Arguments to Main

 using System; using System.IO; using System.Net; class Program {   static int Main(string[] args)                                    {       int result;                                                      string targetFileName = null;       switch (args.Length)                                              {           case 0:               // No URL specified, so display error.               Console.WriteLine(                   "ERROR: You must specify the "                   + "URL to be downloaded");               break;           case 1:               // No target filename was specified.               targetFileName = Path.GetFileName(args[0]);                               break;           case 2:               targetFileName = args[1];                                          break;       }       if (targetFileName != null)       {           WebClient webClient = new WebClient();           webClient.DownloadFile(args[0], targetFileName);                  result = 0;       }             else       {           Console.WriteLine(               "Downloader.exe <URL> <TargetFileName>");           result = 1;       }       return result;                                                  } } 

The results of Listing 4.10 appear in Output 4.4.

Output 4.4.

 >Downloader.exe ERROR:     You must specify the URL to be downloaded Downloader.exe <URL> <TargetFileName> 

If you were successful in calculating the target filename, you would use it to save the downloaded file. Otherwise, you would display the help text. The Main() method also returns an int rather than a void. This is optional for a Main() declaration, but if it is used, the program can return a status code to a caller, such as a script or a batch file. By convention, a return other than zero indicates an error.

Although all command-line arguments can be passed to Main() via an array of strings, sometimes it is convenient to access the arguments from inside a method other than Main(). The System.Environment.GetCommandLineArgs() method returns the command-line arguments array in the same form that Main(string[] args) passes the arguments into Main().

Advanced Topic: Disambiguate Multiple Main() Methods

If a program includes two classes with Main() methods, it is possible to specify on the command line which class to use for the Main() declaration.csc.exe includes a /m option to specify the fully qualified class name of Main().


Beginner Topic: Call Stack and Call Site

As code executes, methods call more methods that in turn call additional methods, and so on. In the simple case of Listing 4.4, Main() calls GetUserInput(), which in turn calls System.Console.ReadLine(), which in turn calls even more methods internally. The set of calls within calls within calls, and so on, is termed the call stack. As program complexity increases, the call stack generally gets larger and larger as each method calls another method. As calls complete, however, the call stack shrinks until another series of methods are invoked. The term for describing the process of removing calls from the call stack is stack unwinding. Stack unwinding always occurs in the reverse order of the method calls. The result of method completion is that execution will return to the call site, which is the location from which the method was invoked.





Essential C# 2.0
Essential C# 2.0
ISBN: 0321150775
EAN: 2147483647
Year: 2007
Pages: 185

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