views:

259

answers:

2

New console project template creates a Main method like this:

class Program
{
    static void Main(string[] args)
    {
    }
}

Why is it that neither the Main method nor the Program class need to be public?

+6  A: 

The Main method shouldn't need to be called by anyone.

It is actually marked as the entry point for execution in the EXE itself, and therefore has no outside callers by default.

If you WANT, you can open it up to be called by marking public, e.g. if you are turning a console app into an API.

John Gietzen
+12  A: 

The entry point of a program is marked with the .entrypoint IL directive. It does not matter if the method or the class is public or not, all that matters is this directive.

dtb
That also means you don't even have to call the main method "Main". The C# Compiler enforces that, but other .net languages can use whatever they want.
Michael Stum