Accepting Command-Line Input
In the previous example, you simply ran the program and it produced output. However, many programs are written to accept command-line input. This makes it easier to write automated scripts that can invoke your program and pass information to it. If you look at many of the programs, including Windows OS utilities, that you use everyday; most of them have some type of command-line interface. For example, if you type Notepad.exe MyFile.txt (assuming the file exists), then the Notepad program will open your MyFile.txt file so you can begin editing it. You can make your programs accept command-line input also, as shown in Listing 1-2, which shows a program that accepts a name from the command line and writes it to the console.
Note: When running the NamedWelcome.exe application in Listing 1-2, you must supply a command-line argument. For example, type the name of the program, followed by your name: NamedWelcome YourName. This is the purpose of Listing 1-2 - to show you how to handle command-line input. Therefore, you must provide an argument on the command-line for the program to work. If you are running Visual Studio, right-click on the project in Solution Explorer, select Properties, click the Debug tab, locate Start Options, and type YourName into Command line arguments. If you forget to to enter YourName on the command-line or enter it into the project properties, as I just explained, you will receive an exception that says "Index was outside the bounds of the array." To keep the program simple and concentrate only on the subject of handling command-line input, I didn't add exception handling. Besides,
Listing 1-2. Getting Command-Line Input: NamedWelcome.cs
// Namespace Declaration
using System;
// Program start classclass NamedWelcome
{ // Main begins program execution. static void Main(string[] args)
{ // Write to console Console.WriteLine("Hello, {0}!", args[0]);
Console.WriteLine("Welcome to the C# Station Tutorial!");
}
}
In Listing 1-2, you'll notice an entry in the Main method's parameter list. The parameter name is args, which you'll use to refer to the parameter later in your program. The string[] expression defines the type of parameter that args is. The string type holds characters. These characters could form a single word, or multiple words. The "[]", square brackets denote an Array, which is like a list. Therefore, the type of theargs parameter, is a list of words from the command-line. Anytime you add string[] args to the parameter list of the Main method, the C# compiler emits code that parses command-line arguments and loads the command-line arguments into args. By reading args, you have access to all arguments, minus the application name, that were typed on the command-line.
You'll also notice an additional Console.WriteLine(...) statement within the Main method. The argument list within this statement is different than before. It has a formatted string with a "{0}" parameter embedded in it. The first parameter in a formatted string begins at number 0, the second is 1, and so on. The "{0}" parameter means that the next argument following the end quote will determine what goes in that position. Hold that thought, and now we'll look at the next argument following the end quote.
The args[0] argument refers to the first string in the args array. The first element of an Array is number 0, the second is number 1, and so on. For example, if I typed NamedWelcome Joe on the command-line, the value of args[0] would be "Joe". This is a little tricky because you know that you typed NamedWelcome.exe on the command-line, but C# doesn't include the executable application name in the args list - only the first parameter after the executable application.
Returning to the embedded "{0}" parameter in the formatted string: Since args[0] is the first argument, after the formatted string, of the Console.WriteLine() statement, its value will be placed into the first embedded parameter of the formatted string. When this command is executed, the value of args[0], which is "Joe" will replace "{0}" in the formatted string. Upon execution of the command-line with "NamedWelcome Joe", the output will be as follows:
Hello, Joe!
Welcome to the C# Station Tutorial!
Visit next day ------>>>>>>