Main Method In C#

The Main method is the entry point of a C# console application or windows application. When the application is started, the Main method is the first method that is invoked. This Main() method is present in every executable application. Executable means any Console application, Windows desktop application or Windows service application.

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.
So,

  • 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.

    Example:

    • It can return void, like,
    1. static void Main()  
    2. {  
    3. //...  
    4. }  
    OR
    1. static void Main(string[] args)  
    2. {  
    3. //...  
    4. }
    • It can also return an integer, like
    1. static int Main()  
    2. {  
    3. //...  
    4. return 0;  
    5. }  
    OR
    1. static int Main(string[] args)  
    2. {  
    3. //...  
    4. return 0;  
    5. }  

Main method define as 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.

FAQ related to this article

  1. Can we have more than one main method in a class?

    Yes, in .NET, an executable can have only one entry point i.e. only one Main method is allowed. Main method is considered as an entry point only if the signature would be static Main(String[]) or static Main(), If you provide any main method whose signature is different from both method mention above, it is not considered as Main method (entry point). So, below code is allowed,
    1. class Program  
    2. {  
    3. static void Main () //Entry point  
    4. {  
    5. }   
    6. static void Main(int number)  
    7. {  
    8. }  
    9. }  
    The below code doesn't compile because it finds matching signature at two places.
    1. class Program  
    2. {  
    3. static void Main () //Entry point  
    4. {  
    5. }   
    6.   
    7. static void Main(String[] args) //another entrypoint!!! Compile Error  
    8. {  
    9. }  
    10. }  
    Below code also doesn't compile because there is no entry point at all,
    1. class Program  
    2. {  
    3. static void Main (int a) //Not an Entry point   
    4. {  
    5. }   
    6.   
    7. static void Main(float b) //Not an entry point  
    8. {  
    9. }  
    10. }  
  2. Can we overload the main method?

    Yes, but only one method would be treated as an entry point. We have already one entry point in that class then other method should not be static Main(String[]) or static Main().

  3. Can we override the main method?

    No, you cannot override the main method because it is a static method and static method can’t be virtual or abstract.

  4. Can we have more than one main method in a program/.exe?

    Yes,If more one class that has a Main method in any .exe, then you must compile your program with the /main compiler option to specify which Main method to use as the entry point.

    csc filename.cs /main:

    <className>
Ebook Download
View all
Learn
View all