What are the default access modifiers in C#?

后端 未结 9 1366
暖寄归人
暖寄归人 2020-11-22 03:44

What is the default access modifier for classes, methods, members, constructors, delegates and interfaces?

相关标签:
9条回答
  • 2020-11-22 03:48
    top level class: internal
    method: private
    members (unless an interface or enum): private (including nested classes)
    members (of interface or enum): public
    constructor: private (note that if no constructor is explicitly defined, a public default constructor will be automatically defined)
    delegate: internal
    interface: internal
    explicitly implemented interface member: public!
    
    0 讨论(0)
  • 2020-11-22 03:49

    Short answer: minimum possible access (cf Jon Skeet's answer).

    Long answer:

    Non-nested types, enumeration and delegate accessibilities (may only have internal or public accessibility)

                         | Default   | Permitted declared accessibilities
    ------------------------------------------------------------------
    namespace            | public    | none (always implicitly public)
    
    enum                 | public    | public, internal
    
    interface            | internal  | public, internal
    
    class                | internal  | public, internal
    
    struct               | internal  | public, internal
    
    delegate             | internal  | public, internal
    

    Nested type and member accessiblities

                         | Default   | Permitted declared accessibilities
    ------------------------------------------------------------------
    namespace            | public    | none (always implicitly public)
    
    enum                 | public    | All¹
    
    interface            | public    | All¹
    
    class                | private   | All¹
    
    struct               | private   | public, internal, private²
    
    delegate             | private   | All¹
    
    constructor          | private   | All¹
    
    enum member          | public    | none (always implicitly public)
    
    interface member     | public    | none (always implicitly public)
         
    method               | private   | All¹
    
    field                | private   | All¹
    
    user-defined operator| none      | public (must be declared public)
    

    ¹ All === public, protected, internal, private, protected internal

    ² structs cannot inherit from structs or classes (although they can, interfaces), hence protected is not a valid modifier

    The accessibility of a nested type depends on its accessibility domain, which is determined by both the declared accessibility of the member and the accessibility domain of the immediately containing type. However, the accessibility domain of a nested type cannot exceed that of the containing type.

    Note: CIL also has the provision for protected and internal (as opposed to the existing protected "or" internal), but to my knowledge this is not currently available for use in C#.


    See:

    http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx
    http://msdn.microsoft.com/en-us/library/ms173121.aspx
    http://msdn.microsoft.com/en-us/library/cx03xt0t.aspx
    (Man I love Microsoft URLs...)

    0 讨论(0)
  • 2020-11-22 03:50

    Class is Internal by default.

    • Class members are private by default.

    Interface is Internal by default.

    • Interface members are public by default. (Interfaces won't allow us to specify any kind of accessibility to it's members.)

      Note: If you try to specify any access specifier to interface's members then, it shows compile error.

    Struct is Internal by default.

    • Struct members are private by default.
    0 讨论(0)
  • 2020-11-22 03:51

    Have a look at Access Modifiers (C# Programming Guide)

    Class and Struct Accessibility

    Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

    Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.

    Derived classes cannot have greater accessibility than their base types. In other words, you cannot have a public class B that derives from an internal class A. If this were allowed, it would have the effect of making A public, because all protected or internal members of A are accessible from the derived class.

    You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute. For more information, see Friend Assemblies.

    Class and Struct Member Accessibility

    Class members (including nested classes and structs) can be declared with any of the six types of access. Struct members cannot be declared as protected because structs do not support inheritance.

    Normally, the accessibility of a member is not greater than the accessibility of the type that contains it. However, a public member of an internal class might be accessible from outside the assembly if the member implements interface methods or overrides virtual methods that are defined in a public base class.

    The type of any member that is a field, property, or event must be at least as accessible as the member itself. Similarly, the return type and the parameter types of any member that is a method, indexer, or delegate must be at least as accessible as the member itself. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private.

    User-defined operators must always be declared as public and static. For more information, see Operator overloading.

    Finalizers cannot have accessibility modifiers.

    Other Types

    Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access. Interface members are always public because the purpose of an interface is to enable other types to access a class or struct. No access modifiers can be applied to interface members.

    Enumeration members are always public, and no access modifiers can be applied.

    Delegates behave like classes and structs. By default, they have internal access when declared directly within a namespace, and private access when nested.

    0 讨论(0)
  • 2020-11-22 03:52

    Namespace level: internal

    Type level: private

    0 讨论(0)
  • 2020-11-22 03:58

    I would like to add some documentation link. Check out more detail here.

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