In C#, what is the difference between public, private, protected, and having no access modifier?

前端 未结 16 2748
盖世英雄少女心
盖世英雄少女心 2020-11-22 01:12

All my college years I have been using public, and would like to know the difference between public, private, and protected

相关标签:
16条回答
  • 2020-11-22 01:52

    A graphical overview (summary in a nutshell)

    Visibility

    Since static classes are sealed, they cannot be inherited (except from Object), so the keyword protected is invalid on static classes.



    For the defaults if you put no access modifier in front, see here:
    Default visibility for C# classes and members (fields, methods, etc.)?

    Non-nested

    enum                              public
    non-nested classes / structs      internal
    interfaces                        internal
    delegates in namespace            internal
    class/struct member(s)            private
    delegates nested in class/struct  private
    

    Nested:

    nested enum      public
    nested interface public
    nested class     private
    nested struct    private
    

    Also, there is they sealed-keyword, which makes a class not-inheritable.
    Also, in VB.NET, the keywords are sometimes different, so here a cheat-sheet:

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

    Hmm.

    See here: Access Modifiers.

    In a nutshell:

    Public gives the method or type complete visibility from other types/classes.

    Private allows only the type containing the private method/variable access to the private method/variable (note that nested classes also have access to the containing classes private methods/variables).

    Protected is similar to private except derived classes can also access protected methods.

    "Nothing" is VB.NET's equivalent to null. Although if you're referring to "nothing" meaning "no access modifier", then it depends, although a very rough rule of thumb (certainly in C#) is that if you don't explicitly specify an access modifier, the method/variable declaration is usually as restricted as it can be. i.e.

    public class MyClass
    {
        string s = "";
    }
    

    is effectively the same as:

    public class MyClass
    {
        private string s = "";
    }
    

    The linked MSDN article will offer a fully description when there's no access modifier explicitly specified.

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

    All access modifiers' descriptions for C#

    0 讨论(0)
  • 2020-11-22 01:59

    Public - If you can see the class, then you can see the method

    Private - If you are part of the class, then you can see the method, otherwise not.

    Protected - Same as Private, plus all descendants can also see the method.

    Static (class) - Remember the distinction between "Class" and "Object" ? Forget all that. They are the same with "static"... the class is the one-and-only instance of itself.

    Static (method) - Whenever you use this method, it will have a frame of reference independent of the actual instance of the class it is part of.

    0 讨论(0)
  • 2020-11-22 02:01

    C# has, in total, 6 access modifiers:

    private: The member declared with this accessibility can be visible within the containing type, it is not visible to any derived types, other types in the same assembly or types outside of the containing assembly. i.e., access is limited to the containing type only.

    protected: The member declared with this accessibility can be visible within the types derived from the containing type within the containing assembly, and the types derived from the containing type outside of the containing assembly. i.e., access is limited to derived types of the containing type.

    internal: The member declared with this accessibility can be visible within the assembly containing this member, it is not visible to any assembly outside of the containing assembly. i.e., access is limited to containing assembly only.

    internal protected: The member declared with this accessibility can be visible within the types derived from the containing type within or outside of the containing assembly, it is also visible to any types within the containing assembly. i.e., access is limited to containing assembly or derived types.

    public: The member declared with this accessibility can be visible within the assembly containing this member, or any other assembly that references the containing assembly. i.e., access is not limited.

    In C# 7.2, a new level of accessibility was added:

    private protected: The member declared with this accessibility can be visible within the types derived from this containing type within the containing assembly. It is not visible to any types not derived from the containing type, or outside of the containing assembly. i.e., the access is limited to derived types within the containing assembly.

    Source including a sample code of the new private protected access modifier

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

    Yet another visual approach of the current access modifier (C# 7.2). Hopefully the schema helps to remember it easier
    (click the image for interactive view.)

    Outside Inside

    If you struggle to remember the two-worded access modifiers, remember outside-inside.

    • private protected: private outside (the same assembly) protected inside (same assembly)
    • protected internal: protected outside (the same assembly) internal inside (same assembly)
    0 讨论(0)
提交回复
热议问题