why is Main not public in C#, in java it gives an error if we don't have main as public, how does the system call the Main in C#?
Asked
Active
Viewed 110 times
1
-
The spec says it can. – leppie Dec 14 '15 at 11:34
-
@user1666620 The main method is a method, which has a default of **private**. See [here](http://stackoverflow.com/a/2521492/261050) – Maarten Dec 14 '15 at 11:34
-
@Maarten thanks for the correction. – user1666620 Dec 14 '15 at 11:36
-
1Well, of course it *should* be private. Calling Main() is pretty unlikely to turn out well. – Hans Passant Dec 14 '15 at 11:36
-
@HansPassant, that's an interesting way of looking at it. Never thought of making `Main` private before, but what you say makes sense. – David Arno Dec 14 '15 at 11:58
1 Answers
-1
From the source:
The CLR doesn't actually mind if your method is private or public. It doesn't invoke it through the use of normal C# code. Here's what the native call stack looks like if you're interested:
00000000`0014ea10 00000642`7f67d4a2 0x642`80150142 00000000`0014ea90 00000642`7f5108f5 mscorwks!CallDescrWorker+0x82 00000000`0014eae0 00000642`7f522ff6 mscorwks!CallDescrWorkerWithHandler+0xe5 00000000`0014eb80 00000642`7f49a94b mscorwks!MethodDesc::CallDescr+0x306 00000000`0014edb0 00000642`7f474ae4 mscorwks!ClassLoader::RunMain+0x23f 00000000`0014f010 00000642`7f5efb1a mscorwks!Assembly::ExecuteMainMethod+0xbc 00000000`0014f300 00000642`7f467d97 mscorwks!SystemDomain::ExecuteMainMethod+0x492 00000000`0014f8d0 00000642`7f482c24 mscorwks!ExecuteEXE+0x47
The address at the top is your Main method.

Rahul Tripathi
- 168,305
- 31
- 280
- 331