tags:

views:

353

answers:

9

Quick question, is there a way to call your main method whatever you like ? Or does it have to be called "Main()" ?

+1  A: 

Nope, it has to be called Main. See Main() and Command Line Arguments (C# Programming Guide).

Kevin
@Kevin: it's best to supply MSDN links without the version number in them, unless the answer is version-specific.
John Saunders
@John Thanks, I always make that mistake when copying a link.
Kevin
+1  A: 

It has to be called Main().

Charles
+1  A: 

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:

Main (C#)

Justin Niessner
Apparently this is a C# convention, not a .NET Framework convention.
John Buchanan
@Justin: Did you realize you supplied a .NET 1.1 link? I've fixed it to not have the version number.
John Saunders
+1  A: 

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);
}
Andy White
Any reason for making Main public? MSDN says it shouldn't be public...
IVlad
+1  A: 

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

Joel Martinez
+1  A: 

From "Main () and Other Methods (C# vs Java)":

Every C# application must contain a single Main method specifying where program execution is to begin.

brydgesk
@brydgesk: please try to supply MSDN links without the version number in them. They will always be current.
John Saunders
+5  A: 

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.
Mark Byers
@Mark: it's best to supply MSDN links without the version number, since they will always refer to the current version.
John Saunders
+16  A: 

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
        }
    }
}
Jesse C. Slicer
Thanks great answer. Really interesting
Chilln
You're very welcome, thanks for the kudos. As an afterthought, I wonder if a tool like PostSharp could inject the appropriate part of the IL (.entrypoint) into a method that was decorated with a particular attribute and therefore obviate the C# "limitation" of the entry point needing to be named Main().
Jesse C. Slicer
+3  A: 

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.

stonemetal