What are the default access modifiers in C#?

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

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

相关标签:
9条回答
  • 2020-11-22 04:04

    Simplest answer is the following.....

    All members in C# always take the LEAST accessible modifier possible by default.

    That is why all top level classes in an assembly are "internal" by default, which means they are public to the assembly they are in, but private or excluded from access to outside assemblies. The only other option for a top level class is public which is more accessible. For nested types its all private except for a few rare exceptions like members of enums and interfaces which can only be public. Some examples. In the case of top level classes and interfaces, the defaults are:

    class Animal same as internal class Animal

    interface Animal same as public interface Animal

    In the case of nested classes and interfaces (inside types), the defaults are:

    class Animal same as private class Animal

    interface Animal same as private interface Animal

    If you just assume the default is always the most private, then you do not need to use an accessors until you need to change the default. Easy.

    0 讨论(0)
  • 2020-11-22 04:04

    Internal is the default modifier

    0 讨论(0)
  • 2020-11-22 04:05

    The default access for everything in C# is "the most restricted access you could declare for that member".

    So for example:

    namespace MyCompany
    {
        class Outer
        {
            void Foo() {}
            class Inner {}
        }
    }
    

    is equivalent to

    namespace MyCompany
    {
        internal class Outer
        {
            private void Foo() {}
            private class Inner {}
        }
    }
    

    The one sort of exception to this is making one part of a property (usually the setter) more restricted than the declared accessibility of the property itself:

    public string Name
    {
        get { ... }
        private set { ... } // This isn't the default, have to do it explicitly
    }
    

    This is what the C# 3.0 specification has to say (section 3.5.1):

    Depending on the context in which a member declaration takes place, only certain types of declared accessibility are permitted. Furthermore, when a member declaration does not include any access modifiers, the context in which the declaration takes place determines the default declared accessibility.

    • Namespaces implicitly have public declared accessibility. No access modifiers are allowed on namespace declarations.
    • Types declared in compilation units or namespaces can have public or internal declared accessibility and default to internal declared accessibility.
    • Class members can have any of the five kinds of declared accessibility and default to private declared accessibility. (Note that a type declared as a member of a class can have any of the five kinds of declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)
    • Struct members can have public, internal, or private declared accessibility and default to private declared accessibility because structs are implicitly sealed. Struct members introduced in a struct (that is, not inherited by that struct) cannot have protected or protected internal declared accessibility. (Note that a type declared as a member of a struct can have public, internal, or private declared accessibility, whereas a type declared as a member of a namespace can have only public or internal declared accessibility.)
    • Interface members implicitly have public declared accessibility. No access modifiers are allowed on interface member declarations.
    • Enumeration members implicitly have public declared accessibility. No access modifiers are allowed on enumeration member declarations.

    (Note that nested types would come under the "class members" or "struct members" parts - and therefore default to private visibility.)

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