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

后端 未结 4 2027
太阳男子
太阳男子 2021-01-21 01:30

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 visi

相关标签:
4条回答
  • 2021-01-21 01:46

    You're right,

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

    0 讨论(0)
  • 2021-01-21 01:52

    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/

    0 讨论(0)
  • 2021-01-21 01:59

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

    0 讨论(0)
  • 2021-01-21 02:01

    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
    
    0 讨论(0)
提交回复
热议问题