The Main() Function

Earlier, you saw that Main() is the entry point for a C# application and that execution of this function encompasses the execution of the application. That is, when execution is initiated, the Main() function executes, and when the Main() function finishes, execution ends. The Main() function can return either void or int , and can optionally include a string[] args parameter, so you can use any of the following versions:

static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)

The third and fourth versions return an int value, which can be used to signify how the application terminates, and often is 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 optional args parameter of Main() provides you with a way to obtain 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 can often specify information directly, such as a file to load on application execution. For example, consider the Notepad application in Windows. You can run Notepad 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 is that Notepad will either 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 similarly by making use of the args parameter.