Main Method in C#
Last Updated :
11 Nov, 2021
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:
using System;
class GFG {
static public void Main(String[] args)
{
Console.WriteLine( "Main Method" );
}
}
|
Output:
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 identifier. [] 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:
using System;
class GFG {
static public void Main()
{
Console.WriteLine( "Main Method" );
}
}
|
Output:
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:
using System;
class GFG {
protected static void Main()
{
Console.WriteLine( "Main Method" );
}
}
|
Output:
Main Method
Example:
using System;
class GFG {
private protected static void Main()
{
Console.WriteLine( "Main Method" );
}
}
|
Compiler Error:
More than one protection modifier specified
- Without any access modifier: The default access modifier is private for a Main() method.
Example:
using System;
class GFG {
static void Main()
{
Console.WriteLine( "Main Method" );
}
}
|
Output:
Main Method
- Order of Modifiers: The user can also swap positions of static and applicable modifiers in Main() method.
Example:
using System;
class GFG {
public static void Main()
{
Console.WriteLine( "Main Method" );
}
}
|
Output:
Main Method
Example:
using System;
class GFG {
static internal void Main()
{
Console.WriteLine( "Main Method" );
}
}
|
Output:
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:
using System;
class GFG {
static int Main()
{
Console.WriteLine( "Main Method" );
return 0;
}
}
|
Output:
Main Method
Example:
using System;
class GFG {
static int Main(String[] args)
{
Console.WriteLine( "Main Method" );
return 0;
}
}
|
Output:
Main Method
Important Points:
Share your thoughts in the comments
Please Login to comment...