C# Default access modifier of Main() method

后端 未结 6 1548
深忆病人
深忆病人 2021-01-12 05:43

I create a sample class in vs2010.

Through Class View, I see the default access modifier for Main is internal.

I also see some people say that the default ac

6条回答
  •  借酒劲吻你
    2021-01-12 06:36

    If your code appears like this:

    static void Main()
    

    then that's a private method. (The static part is orthogonal to accessibility, but is necessary to be an entry point.) In general, the default accessibility of any member is the most private accessibility that you could declare it. So for methods in a class or a struct, that's private. For top-level (non-nested) types it's internal. For any member declared in a class/struct, it's private1. For interface and enum members, it's public.

    It's hard to understand exactly what you're seeing via Class View without seeing either your code or a screenshot of Class View, but the default accessibility for a method is definitely private. That's true regardless of whether it's the Main method or not.


    1 Explicit interface implementation is a bit odd here, as it's neither private nor public; it's simply not accessible through the type, only through the interface.

提交回复
热议问题