C# Way to name Main() method by yourself?

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
        }
    }
}

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.

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.

Tags:

C#

Main