C# Default access modifier of Main() method

后端 未结 6 1551
深忆病人
深忆病人 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:19

    You can't see the default access modifier for a member in the class browser, you can see the actual access modifier.

    The default access modifiers for classes at the namespace level is internal, whereas the default access modifier for class members (including nested classes) is private. There's no special case for the Main() function. If there's no access modifier before it (a la Jon Skeet's example), then it's private. If there is one, then that's what it is.

    0 讨论(0)
  • 2021-01-12 06:20

    Although you tagged your question c#, let me say that the access modifiers for the default Program.Main generated by VS2010 actually depends on the project template, on these differ for each language. I quickly tried the following:

    • In a VB.NET console project, the Program module (static class) is Friend (i.e. internal in C#) and the Main static method is Public.

    • In a C# console project, Program is internal, and Main is private.

    That is, a C# project will simply use the default access modifiers (internal for classes, private for methods).

    0 讨论(0)
  • 2021-01-12 06:22

    Both, the default class modifier is internal. The main method is a method and is private. In general, classes without a modifier are internal, class-members (such as methods and fields) without a declaration are private.

    0 讨论(0)
  • 2021-01-12 06:33

    Private members are accessible only within the body of the class in which they are declared.

    Internal types or members are accessible only within files in the same assembly

    Internal 'is like' public but only for all elements of the same assembly. Class1 of assembly1 cannot 'see' or access any internal element of assembly2.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 06:38

    By Default the access specifier for Main() in C# is private.

    This is what I got when I saw the MSIL(IL) code in ILDASM.

    You can see that Main() is private. IL code of Simple Program in C#

    0 讨论(0)
提交回复
热议问题