Returns and Parameters on Main Method
So far, declaration of an executable’s Main method has been the simplest declaration possible. You have not included any parameters or non-void return type 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—named args by convention. All you need to do to retrieve the parameters is to access the array, as demonstrated in Listing 5.16 with Output 5.3. The output shows a run with no parameters and a second run with two parameters. The purpose of this program is to download a file whose location is specified by a URL. The first command-line argument identifies the URL, and the 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:
If there are not two parameters, display an error indicating that it is necessary to provide the URL and filename.
The presence of two arguments indicates the user has provided both the URL of the resource and the download target filename.
Listing 5.16: Passing Command-Line Arguments to a Main Method
public class Program { public static int Main(string[] args) { int result; if(args?.Length != 2 ) { // Exactly two arguments must be specified; give an error Console.WriteLine( "ERROR: You must specify the " + "URL and the file name"); Console.WriteLine( "Usage: Downloader.exe <URL> <TargetFileName>"); result = 1; } else { string urlString = args[0]; string fileName = args[1]; HttpClient client = new(); byte[] response = client.GetByteArrayAsync(urlString).Result; client.Dispose(); File.WriteAllBytes(fileName, response); Console.WriteLine($"Downloaded '{ fileName }' from '{ ↪urlString }'."); result = 0; } return result; } }
Output 5.3
>Downloader ERROR: You must specify the URL and the file name Usage: Downloader.exe <URL> <TargetFileName> >Downloader https://IntelliTect.com Index.html Downloaded ‘Index.html’ from ‘https://IntelliTect.com’.
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 method declarations, but if it is used, the program can return an exit 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 the Main method except that the first item in the array is the executable filename.
To download the file, Listing 5.16 uses a System.Net.Http.HttpClient object but only specified HttpClient–its namespace is imported by the ImplicitUsings element in the .csproj file.