C# Protected Internal

I’ve noticed that most of the c# developers i’am talking to, know exactly what “public”, “private”, “protected” and “internal” access modifiers mean and how to use them, but when it comes to “protected internal” they start guessing the answer and it’s never the right one.

Well, let’s make some order in that.

protected
A protected member is accessible within its class and by derived classes.

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

protected internal
Access is limited to the current assembly or types derived from the containing class.
* protected internal is the only access modifiers combination allowed for a member or a type.

So, as we can see “protected internal” can be used in the same assembly or types derived from the containing class in any assembly.

It means that we can access a “protected internal” method by accessing any instance of it’s class in the same assembly, in any class in different assembly which derives from the class, but we won’t be able to access the method by accessing an instance of it’s class in different assembly.

// Assemby : A
namespace AssemblyA
{
    public class A
    {
        protected internal string SomeProtectedInternalMethod() {
            return "SomeValue";
        }
    }

    public class A2 : A
    {
        public string SomeMethod() {
            // We can access the method because
            // it's protected and inherited by A2
            return SomeProtectedInternalMethod();
        }
    }

    class A3 : A
    {
        public string SomeMethod()
        {
            A AI = new A();
            // We can access the method through an instance
            // of the class because it's internal
            return AI.SomeProtectedInternalMethod();
        }
    }
}
// Assemby : B
using AssemblyA;
namespace AssemblyB
{
    class B : A
    {
        public string SomeMethod() {
            // We can access the method because
            // it's inherited by A2
            // despite the different assembly
            return SomeProtectedInternalMethod();
        }
    }

    class B2
    {
        public string SomeMethod()
        {
            A AI = new A();
            // We can't access the method
            // through the class instance
            // because it's different assembly
            return AI.SomeProtectedInternalMethod();
        }
    }
}

So, if we will try to compile AssemblyB we’ll get an error in line 23 that says : AssemblyA.A.SomeProtectedInternalMethod()’ is inaccessible due to its protection level

I hope this explains once and for all the meaning of “protected internal” access modifier.

VN:F [1.9.22_1171]
Rating: 4.8/5 (17 votes cast)
C# Protected Internal, 4.8 out of 5 based on 17 ratings
  1. Edwin
    August 11th, 2009 at 20:28 | #1

    Simple & great explanation,Thanks!

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  2. Mahesh
    September 3rd, 2009 at 15:45 | #2

    Great article. Thank you.

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  3. C# Handler
    September 26th, 2009 at 17:16 | #3

    Simply speaking ….
    Protected internal Removing restriction of one another and hence come out with different accessibility specifier .
    Means..protected provide accessibility outside the assembly also..
    & internal provide accessibility to any class within the assembly even it is not derived from the mention class ..
    One can’t say, the use of protected internal is redundant…
    because its a different accessibility specifier than public,private, internal,protected..(just match)..:)

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  4. Sathya
    November 26th, 2009 at 09:05 | #4

    Good and simple explanation

    Thanks
    Sathya

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  5. Suresh
    February 22nd, 2010 at 13:36 | #5

    Both Protected and ProtectedInternal members can be accessed outside the assembly by
    inheriting the contained Class.

    internal – can be accessed with in the assembly anywhere by Inheriting/Creating instance.
    protected – can be accessed only by Inheriting with in the Assembly/Outside the Assembly.
    ProtectedInternal – can be accessed with in the assembly anywhere by Inherting/Creating instance AND accessed outside the assembly by only Inherting the Contained Class

    so protectedInternal behaves like both protected or internal.

    Thanks
    Suresh Raj

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  6. Shemeer NS
    March 18th, 2010 at 10:38 | #6

    nice presentation

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  7. Artem
    September 26th, 2010 at 15:08 | #7

    1. class A3 : A

    Class A3 don’t have to inherit from A in your example. If it does, then you can access protected members even on another instance, like this:

    A AI = new A();
    return ((A3)AI).SomeProtectedInternalMethod();

    Within or outside of an assembly in which class A is declared.

    2. // We can access the method through an instance
    // of the class because it’s internal

    Not exactly right. We can access it just because it is in the same assembly.

    The point is that you don’t need to declare class A3 as internal (as you did), this has absolutely nothing to do with the access to class A internal members.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  8. September 29th, 2010 at 17:40 | #8

    @Artem
    Hi, Artem

    1. You are correct that we don’t have to inherit A3 from A to be able access SomeProtectedInternalMethod from the same assembly (thanks for noticing, probably just mistyped it), but you are wrong about using ((A3)AI).SomeProtectedInternalMethod() within or outside of an assembly in which class A is declared. You indeed can use it using casting but in the SAME assembly only.

    2. You can’t access a method in some class just because the class is in the same assembly.
    For example if the method was defined as “protected string SomeProtectedInternalMethod()” or just “string SomeProtectedInternalMethod()” you wouldn’t be able to access it even in the same assembly because in case of protected you must inherit the class and in the second case it’s just private for the same class.

    VN:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  9. Artem
    October 3rd, 2010 at 12:45 | #9

    @Xander

    1. Actually I was wrong because you can’t cast A to A3.

    2. I misinterpret your comment. What I meant is that class A3 can access SomeProtectedInternalMethod of class A because SomeProtectedInternalMethod is internal in class A and A3 is in the same assembly. But you made class A3 internal too, and it sounds like you are saying THIS is the reason why it can access the method. But you are not.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  10. syam
    February 28th, 2011 at 15:17 | #10

    if I compile your code I am Not getting any error it is compiling fine….. So I did not understand

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  11. syam
    February 28th, 2011 at 15:21 | #11

    I dont know where I am going wrong

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  12. Bassam Alugili
    April 14th, 2011 at 13:50 | #12

    Important point:

    For example if u define a method as:

    protected internal void DoSomething()
    {
    Console.WriteLine(“I’m Doing…”);
    }

    Normally, you think this method is protected and internal!!!! <—- THIS IS WRONG !!!!!

    The connection between them is OR = protected internal means protected OR internal

    Example for that if you define an internal property and you try to set the setter or the getter as protected internal you will get a restrictive error because The union of protected and internal accessibility is less restrictive than protected or internal alone.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
  13. Harish
    July 19th, 2011 at 21:46 | #13

    Great Article

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  14. Sanjay Modi
    January 23rd, 2012 at 18:55 | #14

    Really Good . in simple word you explain every thing

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  15. April 4th, 2013 at 10:22 | #15

    Good one…

    VA:F [1.9.22_1171]
    Rating: 5.0/5 (1 vote cast)
  1. July 2nd, 2009 at 23:07 | #1
  2. April 2nd, 2010 at 02:22 | #2

Subscribe without commenting

SEO Powered by Platinum SEO from Techblissonline
watch free movies