By default the type modifier for every member in a class is a private, even the Main() function type modifier is private. How does the CLR call the main method which is not visible to the outside world?
Asked
Active
Viewed 1,029 times
3
-
Maybe through [Reflection](http://en.wikipedia.org/wiki/Reflection_(computer_science)#C.23)? – Uwe Keim Apr 29 '11 at 07:05
-
1By contrast, Java requires its `main` method to be public. It's invoked by the launcher using reflection, so in theory, it's possible to invoke a non-public `main` too, but the launcher actually checks for the public flag and enforces it. (Also by contrast, Java doesn't have `entrypoint`. The method has to be named `main`, has to be static and public, has to return `void` (not `int`), and has to take a single argument of type `String[]`.) – C. K. Young Apr 29 '11 at 07:13
-
2The CLR manages to call other private methods; what makes Main special that it ought to be different? – Eric Lippert Apr 29 '11 at 14:58
4 Answers
7
The CLR does not care about the accessibility of main
. "Visible to the outside world" only applies to the code, not the runtime.

user703016
- 37,307
- 8
- 87
- 112
3
Thats not true.
It has to be public. For e.g. public static void Main()
.
EDIT: Here is what I found & learned today, on why Main
need not be public
.
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/9184c55b-4629-4fbf-ad77-2e96eadc4d62/

shahkalpesh
- 33,172
- 3
- 63
- 88
3
Try using ildasm on your code and lookout for the main method
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint // this is something the CLR is interested in

V4Vendetta
- 37,194
- 9
- 78
- 82
1
You're right,
it's marked as an entrypoint. Check this question: Why is Main method private?

Community
- 1
- 1

CloudyMarble
- 36,908
- 70
- 97
- 130