Quick question, is there a way to call your main method whatever you like ? Or does it have to be called "Main()" ?
Nope, it has to be called Main. See Main() and Command Line Arguments (C# Programming Guide).
It needs to be called Main()
since it's the entry point for the application.
The name and restrictions are defined by the Framework. Here's the MSDN page for reference:
It has to be called Main.
If you really wanted to, you could create your own method, and just call it as the one-and-only line of code in Main.
public static void Main(String[] args)
{
Mane(args);
}
Nope ... Main is standard. What you can do though is change the class that hosts the Main method from the project property's "Startup object" setting
But really, why would you want to? everyone knows that it's main ... so you'd be confusing other developers that may look at your code
From "Main () and Other Methods (C# vs Java)":
Every C# application must contain a single Main method specifying where program execution is to begin.
You can call your main method something else, but it won't be called as the first method in your application unless it is called Main
. There are a few other requirements and things to note too. From MSDN:
- The Main method is the entry point of your program, where the program control starts and ends.
- It is declared inside a class or struct. It must be static and it should not be public.
- It can either have a void or int return type.
- The Main method can be declared with or without parameters.
- Parameters can be read as zero-indexed command line arguments.
- Unlike C and C++, the name of the program is not treated as the first command line argument.
Note this is a C# convention, not a .NET Runtime convention. You can name your method whatever you'd like in IL:
.module Mane.exe
.subsystem 3
.corflags 9
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89)
.ver 2:0:0:0
}
.assembly Mane
{
.custom instance void [mscorlib]System.Runtime.InteropServices.ComVisibleAttribute::.ctor(bool) = ( 01 00 00 00 00 )
.custom instance void [mscorlib]System.CLSCompliantAttribute::.ctor(bool) = ( 01 00 01 00 00 )
.custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 )
.custom instance void [mscorlib]System.Resources.NeutralResourcesLanguageAttribute::.ctor(string) = ( 01 00 05 65 6E 2D 55 53 00 00 )
.permissionset reqmin
= {[mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {property bool 'Execution' = bool(true)}}
.hash algorithm 0x00008004
.ver 1:0:0:0
}
.namespace Mane
{
.class private abstract auto ansi sealed beforefieldinit Program extends [mscorlib]System.Object
{
.method private hidebysig static void Mane() cil managed
{
.entrypoint
.maxstack 1
ldstr "Hello, World!"
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
}
}
I don't believe there is a way to do it on the C# side of things but if you are willing to edit your IL it is easy enough to go in and add .entrypoint to the function in IL. Check out the CLI entry on wikipedia.