Which access modifier more accessible, internal or protected?

This is a discussion on Which access modifier more accessible, internal or protected? within the C# Programming forums, part of the General Programming Boards category; My understanding of C# access modifiers is as follows: public : type/type member accessible from any code inside/outside assembly. protected ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    Which access modifier more accessible, internal or protected?

    My understanding of C# access modifiers is as follows:

    public: type/type member accessible from any code inside/outside assembly.

    protected internal: type/type member accessible from code inside the assembly as well as code inside derived types outside the assembly

    protected: type/type member accessible from code within the containing type as well as code inside derived types inside or outside the assembly

    internal: type/type member accessible only from code inside the assembly

    private: type/type member accessible only from code inside the containing type


    The problem is that I am not 100% which one is more accessible, protected or internal. It could be argued that protected is more accessible, since it grants access to some code outside the assembly. But it could also be argued that internal is more accessible, since it grants access to all code inside the assembly.

    To make matters worse, remember that a field cannot be more accessible than its type, so if you have something like this:

    Code:
    public class A
    {
    protected class B{}
    internal B b;
    }
    You will get a compile-time error message saying that variable b cannot be more accessible than its type B.

    By the same token, if you do this:

    Code:
    public class A
    {
    internal class B{}
    protected B b;
    }
    You should, contradictorily, get the same compile-time error message, saying that variable b cannot be more accessible than its type B.

    On the other hand, you should be able to get away with the following, since variable b is less accessible than its type B.

    Code:
    public class A
    {
    protected internal class B{}
    protected B b;
    }
    Regardless of whether or not I got the above code right, I'm pretty sure that there seems to be some ambiguity concerning the relative accessibility of protected vs. internal.

    Can someone please explain?

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Hmm. I'm going to say protected is more accessible. You can use "protected override" on a method inside an external assembly (eg. overriding a mouse event of a button).

    Internal objects/methods are exactly that, internal. Hidden from the view of external assemblies.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When in doubt, consult the MSDN:

    private (C#) - "The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared. "

    protected (C#) - "The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member."

    internal (C#) - "The internal keyword is an access modifier for types and type members. Internal members are accessible only within files in the same assembly."

    So basically internal is like using the 'static' keyword on functions in C. It makes them limited to file scope.


    Quzah.
    Last edited by quzah; 10-12-2011 at 06:50 AM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot access protected member declared in X
    By Elysia in forum C++ Programming
    Replies: 14
    Last Post: 03-28-2010, 07:27 AM
  2. error C2248: can not access protected member
    By r0flc0pter in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2009, 10:34 AM
  3. Access to members of protected base class
    By DL1 in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2009, 10:30 AM
  4. access modifier after function name in declaration
    By kes103 in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2003, 11:19 AM
  5. how do u access a class protected area?
    By CwannaB in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 07:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21