Main Method in C#
C# applications have an entry point called Main Method. It is the first method which gets invoked whenever an application started and it is present in every C# executable file. The application may be Console Application or Windows Application. The most common entry point of a C# program is static void Main() or static void Main(String []args).
Different Declaration of Main() Method
Below are the valid declarations of Main Method in a C# program:
- With command line arguments: This can accept n number of array type parameters during the runtime.
Example:
usingSystem;classGFG {// Main MethodstaticpublicvoidMain(String[] args){Console.WriteLine("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
Meaning of the Main Syntax:
static: It means Main Method can be called without an object.
public: It is access modifiers which means the compiler can execute this from anywhere.
void: The Main method doesn’t return anything.
Main(): It is the configured name of the Main method.
String []args: For accepting the zero-indexed command line arguments. args is the user-defined name. So you can change it by a valid identifer. [] must come before the args otherwise compiler will give errors. -
Without Command line arguments: It is up to the user whether he wants to take command line arguments or not. If there is a need for command-line arguments then the user must specify the command line arguments in the Main method.
Example:
usingSystem;classGFG {// Main MethodstaticpublicvoidMain(){Console.WriteLine("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- Applicable Access Modifiers: public, private, protected, internal, protected internal access modifiers can be used with the Main() method. The private protected access modifier cannot be used with it.
Example:
usingSystem;classGFG {// Main MethodprotectedstaticvoidMain(){Console.WriteLine("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
Example:
usingSystem;classGFG {// Main MethodprivateprotectedstaticvoidMain(){Console.WriteLine("Main Method");}}chevron_rightfilter_noneCompiler Error:
More than one protection modifier specified
- Without any access modifier: The default access modifier is private for a Main() method.
Example:
usingSystem;classGFG {// Main Method without any// access modifierstaticvoidMain(){Console.WriteLine("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- Order of Modifiers: The user can also swap positions of static and applicable modifiers in Main() method.
Example:
usingSystem;classGFG {// Main MethodpublicstaticvoidMain(){Console.WriteLine("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
Example:
usingSystem;classGFG {// Main Method with swapping of modifiersstaticinternalvoidMain(){Console.WriteLine("Main Method");}}chevron_rightfilter_noneOutput:
Main Method
- Return Type: The Main Method can also have integer return type. Returning an integer value from Main() method cause the program to obtain a status information. The value which is returned from Main() method is treated as the exit code for the process.
Example:
usingSystem;classGFG {// Main Method with int return typestaticintMain(){Console.WriteLine("Main Method");// for successful execution of codereturn0;}}chevron_rightfilter_noneOutput:
Main Method
Example:
usingSystem;classGFG {// Main Method with int return typestaticintMain(String[] args){Console.WriteLine("Main Method");// for successful execution of codereturn0;}}chevron_rightfilter_noneOutput:
Main Method
Important Points:
- The Main() method is the entry point a C# program from where the execution starts.
- Main() method must be static because it is a class level method. To invoked without any instance of the class it must be static. Non-static Main() method will give a compile-time error.
- Main() Method cannot be overridden because it is the static method. Also, the static method cannot be virtual or abstract.
- Overloading of Main() method is allowed. But in that case, only one Main() method is considered as one entry point to start the execution of the program.
Example: Valid Overloading of Main() Method
// C# program to demonstrate the// Valid overloading of Main()// methodusingSystem;classGFG {// Main method// it can also be written as// static void Main(String []args)staticvoidMain(){Console.WriteLine("Main Method");}// overloaded Main() MethodstaticvoidMain(intn){Console.WriteLine("Overloaded Main Method");}// overloaded Main() MethodstaticvoidMain(intx,inty){Console.WriteLine("Overloaded Main Method");}}chevron_rightfilter_noneOutput:
Main Method
Warnings:
prog.cs(17, 14): warning CS0028: `GFG.Main(int)’ has the wrong signature to be an entry point
prog.cs(23, 14): warning CS0028: `GFG.Main(int, int)’ has the wrong signature to be an entry pointExample: Invalid overloading of Main() Method
// C# program to demonstrate the// Invalid overloading of Main()// methodusingSystem;classGFG {// Main method// it can also be written as// static void Main()staticvoidMain(String[] args){Console.WriteLine("Main Method");}// overloaded Main() MethodstaticvoidMain(){Console.WriteLine("Overloaded Main Method");}}chevron_rightfilter_noneCompile Errors:
prog.cs(11, 14): error CS0017: Program `5c56b8183078e496102b7f2662f8b84e.exe’ has more than one entry point defined: `GFG.Main(string[])’
prog.cs(17, 14): error CS0017: Program `5c56b8183078e496102b7f2662f8b84e.exe’ has more than one entry point defined: `GFG.Main()’ - The allowed type of command line arguments is only String array in the parameters of the Main() method.
- The allowed return type of Main() method is void, int, Task(From C# 7.1), and Task<T>(From C# 7.1).
- Declaration of Main() method may include async modifier if the return type of Main() method is Task or Task<T>.
- Libraries and services in C# do not require a Main() method as an entry point.
- If more than one C# class contains the Main() method then the user must have to compiler the program with /main option to specify which Main() method will be the entry point.
Recommended Posts:
- Main Thread in C#
- Difference between Method Overriding and Method Hiding in C#
- C# | IsNullOrEmpty() Method
- C# | ToLower() Method
- C# | IsNullOrWhiteSpace() Method
- C# | Copy() Method
- C# | Uri.FromHex() Method
- C# | Remove() Method
- C# | ToUpper() Method
- C# | Uri.GetHashCode() Method
- C# | ToCharArray() Method
- C# | Uri.IsHexEncoding() Method
- TimeSpan.Add() Method in C#
- C# | Insert() Method
- C# | Random.Next() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



