The Main() Function


Now that you've covered most of the simple techniques used in the creation and use of functions, it's time to go back and take a closer look at the Main() function.

Earlier, you saw that Main() is the entry point for a C# application and that the execution of this function encompasses the execution of the application. That is to say that when execution is initiated, the Main() function executes, and when the Main() function finishes, execution ends. You also saw that this function has a parameter, string[] args, but I haven't explained what this parameter represents yet. In this section, you see what this parameter is and how you use it.

Note

Note that there are four possible signatures that you can use for the Main() function:

  • static void Main()

  • static void Main(string[] args)

  • static int Main()

  • static int Main(string[] args)

You can, if you wish, omit the args argument discussed here. The reason you've used the version with this argument up till now is that it is the version that is generated automatically for you when you create a console application in VS.

The third and fourth versions shown above return an int value, which can be used to signify how the application terminates, often used as an indication of an error (although this is by no means mandatory). In general, returning a value of 0 reflects normal termination (that is, the application has completed and can terminate safely).

The args parameter of Main() is a method for accepting information from outside the application, specified at runtime. This information takes the form of command-line parameters.

You may well have come across command-line parameters already. When you execute an application from the command line, you are often able to specify information directly, such as a file to load on application execution. As an example, consider the Notepad application in Windows. You can run this application simply by typing Notepad in a command prompt window or in the window that appears when you select the Run option from the Windows Start Menu. You can also type something like Notepad "myfile.txt" in these locations. The result of this is that Notepad will load the file myfile.txt when it runs or offer to create this file if it doesn't already exist. Here, "myfile.txt" is a command-line argument. You can write console applications that work in much the same way by making use of the args parameter.

When a console application is executed, any command-line parameters that are specified are placed in this args array. You can then use these parameters in your application as required.

Here's an example of this in action. In the following Try It Out, you'll be able to specify any number of command-line arguments, each of which will be output to the console.

Try It Out – Command-Line Arguments

image from book
  1. Create a new console application called Ch06Ex04 in the directory C:\BegVCSharp\Chapter6.

  2. Add the following code to Program.cs:

    class Program {    static void Main(string[] args)    { Console.WriteLine("{0} command line arguments were specified:", args.Length); foreach (string arg in args) Console.WriteLine(arg); Console.ReadKey();    } }

  3. Open up the property pages for the project (right-click on the Ch06Ex04 project name in the Solution Explorer window and select Properties).

  4. Select the Debug page and add whatever command-line arguments you want to the Command line arguments setting. An example is shown in Figure 6-7.

    image from book
    Figure 6-7

  5. Run the application. The output is shown in Figure 6-8.

    image from book
    Figure 6-8

How It Works

The code used here is very simple:

Console.WriteLine("{0} command line arguments were specified:",                   args.Length); foreach (string arg in args)    Console.WriteLine(arg);

You're just using the args parameter as you would any other string array. You're not doing anything fancy with the arguments, you're just writing whatever is specified to the screen.

In this example, you supplied the arguments via the project properties in VS. This is a handy way of using the same command-line arguments whenever you run the application from VS, rather than having to type them at a command-line prompt every time. The same result could be obtained by opening a command prompt window in the same directory as the project output (C:\BegCSharp\Chapter6\ Ch06Ex04\bin\Debug) and typing the following:

Ch06Ex04 256 myFile.txt "a longer argument"

Note that each argument is separated from the next by spaces. If you want to supply an argument that includes spaces, you can enclose it in double quotation marks, which will prevent it from being interpreted as multiple arguments.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

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