Why is Main method private?
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?
Solution 1:
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.
Solution 2:
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.