1

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#?

HighNESS
  • 57
  • 1
  • 2
  • 8

1 Answers1

-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