Oct 3, 2013

What is the Main() in C#.net In Detail?



The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.



There can only be one entry point in a C# program. If you have more than one class that has a Main method, you must compile your program with the /main compiler option to specify which Main method to use as the entry point.

 
class TestClass
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments:
        System.Console.WriteLine(args.Length);
    }
}


 
  • The Main method is the entry point of an .exe program; it is where the program control  starts and ends.
  •  Main is declared inside a class or struct. Main must be static 
  •  The enclosing class or struct is not required to be static.
  •  Main can either have a void or int return type.
  •  The Main method can be declared with or without a string[] parameter that contains command-line arguments. When using Visual Studio to create Windows Forms applications, you can add the parameter manually or else use the Environment class to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. 


Why Main() method is static?

Static members are scoped to the class level (rather than the object level) and can thus be invoked without   the need to first create a new class instance. A main method is static because it is available to run when your program starts and as it is the entry point of the program it runs without creating an instance of the class.

In other words, static functions exist before a class is instantiated so static is applied to the main entry point (Main method).

More about Main() method

By default Visual Studio defines Main() method as private. The reason behind this other application can not invoke the entry point (Main() method) of another. However please note that we can declare the Main() method as public.


Return value of Main() method?

Return value of Main() method is void (indicates no return value) by default. We can change it to one of the following possible signatures.
// int return type, array of strings as the parameter.

static int Main(string[] args)
{
  return 0;
}

static void Main()
{
}

static int Main()
{
  return 0;
}
We can return an int value in main method if we want some output when program terminates like whether the program completed execution successfully.
Also we can use string[] args parameter in Main() method if we want to process some user defined command line arguments. We can see this in more detail in below points. What is use of return of int value in Main Method?


We can use int return value of the main method to check whether there is an error occurred in the main method. For ex if main method executes successfully then we will return 0 (this is the default value return by the main method even if it defined as void). If main method executes unsuccessfully then we will return -1.


 What is use of return of int value in Main Method?

 
We can use int return value of the main method to check whether there is an error occurred in the main method. For ex if main method executes successfully then we will return 0 (this is the default value return by the main method even if it defined as void). If main method executes unsuccessfully then we will return -1.

 

No comments:

Post a Comment