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.3_1094]
Rating: 4.8/5 (13 votes cast)
C# Protected Internal, 4.8 out of 5 based on 13 ratings

  • DZone
  • Digg
  • Twitter
  • Yahoo Buzz
  • StumbleUpon
  • Delicious
  • Technorati Favorites
  • LiveJournal
  • Reddit
  • Share/Bookmark

A few posts you might find interesting:

  1. C# Inheritance Question
  2. C# Set method timeout using Generics
  3. C# string.Empty vs “”
  4. C# The “double” trouble
  5. C# LINQ Teaser

  1. Edwin
    August 11th, 2009 at 20:28 | #1

    Simple & great explanation,Thanks!

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

    Great article. Thank you.

    VA:F [1.9.3_1094]
    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.3_1094]
    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.3_1094]
    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.3_1094]
    Rating: 5.0/5 (1 vote cast)
  6. Shemeer NS
    March 18th, 2010 at 10:38 | #6

    nice presentation

    VA:F [1.9.3_1094]
    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