Main Entry Point


Main is the entry point method for a C# application and a member function of a class or struct (the entry point method is where the C# application starts executing). There are four valid signatures of Main being used as the entry point method:

 static void Main() {     // main block } static int Main() {     // main block } static void Main(string [] args){     // main block } static int Main(string [] args){     // } 

Notice that the entry point method must be static.

A class or struct can contain only one valid entry point method. The accessibility of the Main entry point method is irrelevant to the invocation of that method. Even a private Main method is reachable as an entry point method.

Entry point methods can pass command line arguments into an application as a string array. Arrays in .NET derive from System.Array. You can use the properties and methods of System.Array to examine the command line arguments, including the Length field to determine the count of arguments. The command arguments start at element zero of the string array. When no arguments are passed, the arg parameter is non-null but the array count is zero.

The return of an entry point method is cached internally for interprocess communication. If the application is part of a system and spawned to complete a specific task, the return could represent a status code or the result of the task. The default exit code of an application is zero. The exit code of a process is stored in the Process Environment Block (PEB) and readable through the GetExitCodeProcess API.

What if the entry point is ambiguous?

 using System; namespace Application{     class StarterA{         static void Main() {         }     }     class StarterB{         static void Main() {         }     } } 

The preceding code has two valid entry points, which is inherently ambiguous. The main compiler is available to select between multiple entry points. This command successfully compiles the program: csc/main:Application.StarterB main.cs.




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

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