The Main() Method

 
Chapter 2 - C# Basics
bySimon Robinsonet al.
Wrox Press 2002
  

We saw at the start of this chapter that C# programs start execution at a method named Main() . As we 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, since by definition the method must be called from outside the program, it doesn't actually matter what accessibility level we assign to the method it will run even if we 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 listed above, 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 we try to compile this in the usual way we will get the following errors:

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

However, we can explicitly tell the compiler which of these methods to use as the entry point for the program 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()

In our examples so far, we have only shown the Main() method without any parameters. However, when the program is invoked, we 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). We can read this array to evaluate 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;     }     }     }   

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

  ArgsExample /a /b /c  /a /b /c 
  


Professional C#. 2nd Edition
Performance Consulting: A Practical Guide for HR and Learning Professionals
ISBN: 1576754359
EAN: 2147483647
Year: 2002
Pages: 244

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