The Main() Method


You saw at the start of this chapter that C# programs start execution at a method named Main(). As you saw earlier, this must be a static method of a class (or struct), and must have a return type of either int or void.

Although it is common to specify the public modifier explicitly, because by definition the method must be called from outside the program, it doesn’t actually matter what accessibility level you assign to the entry-point method - it will run even if you mark the method as private.

Multiple Main() Methods

When a C# console or Windows application is compiled, by default the compiler looks for exactly one Main() method in any class matching the signature that was just described and makes that class method the entry point for the program. If there is more than one Main() method, the compiler will return an error message. For example, consider the following code called MainExample.cs:

  using System; namespace Wrox.ProCSharp.Basics {    class Client    {       public static int Main()       {          MathExample.Main();          return 0;       }    }    class MathExample    {       static int Add(int x, int y)       {          return x + y;       }       public static int Main()       {          int i = Add(5,10);          Console.WriteLine(i);          return 0;       }    } } 

This contains two classes, both of which have a Main() method. If you try to compile this code in the usual way, you will get the following errors:

csc MainExample.cs

 Microsoft (R) Visual C# .NET Compiler version 8.00.40607.16 for Microsoft (R) Windows (R) .NET Framework version 2.0.40607 Copyright (C) Microsoft Corporation 2001-2003. All rights reserved. MainExample.cs(7,23): error CS0017: Program 'MainExample.exe' has more than one entry point defined: 'Wrox.ProCSharp.Basics.Client.Main()' MainExample.cs(21,23): error CS0017: Program 'MainExample.exe' has more than one entry point defined: 'Wrox.ProCSharp.Basics.MathExample.Main()'

However, you can explicitly tell the compiler which of these methods to use as the entry point for the program by using the /main switch, together with the full name (including namespace) of the class to which the Main() method belongs:

 csc MainExample.cs /main:Wrox.ProCSharp.Basics.MathExample

Passing Arguments to Main()

The examples so far have only shown the Main() method without any parameters. However, when the rogram is invoked, you can get the CLR to pass any command-line arguments to the program by including a parameter. This parameter is a string array, traditionally called args (although C# will accept any name). The program can use this array to access any options passed through the command line when the program is started.

The following sample, ArgsExample.cs, loops through the string array passed in to the Main() method and writes the value of each option to the console window:

  using System; namespace Wrox.ProCSharp.Basics {    class ArgsExample    {       public static int Main(string[] args)       {          for (int i = 0; i < args.Length; i++)          {             Console.WriteLine(args[i]);          }          return 0;       }    } } 

You can compile this as usual using the command line. When you run the compiled executable, you can pass in arguments after the name of the program, for example:

ArgsExample /a /b /c

 /a /b /c




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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