Why does my program work if my Main method in C# is private?

大城市里の小女人 提交于 2019-12-01 21:15:04

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/

The CLR does not care about the accessibility of main. "Visible to the outside world" only applies to the code, not the runtime.

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
CloudyMarble

You're right,

it's marked as an entrypoint. Check this question: Why is Main method private?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!