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,
OR
- static void Main(string[] args)
- {
-
- }
• It can also return an integer, like
- static int Main()
- {
-
- return 0;
- }
OR
- static int Main(string[] args)
- {
-
- return 0;
- }
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
- 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,
- class Program
- {
- static void Main ()
- {
- }
- static void Main(int number)
- {
- }
- }
The below code doesn't compile because it finds matching signature at two places.
- class Program
- {
- static void Main ()
- {
- }
-
- static void Main(String[] args)
- {
- }
- }
Below code also doesn't compile because there is no entry point at all,
- class Program
- {
- static void Main (int a)
- {
- }
-
- static void Main(float b)
- {
- }
- }
- 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().
- 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.
- 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>