Question

I am raising a question (again) on the protected vs protected internal as I am still unsure and not able to get a grip on them.

Any help is much appreciated.

// DLL 1

 namespace Assembly1
  {
    class class1 : class2
    {
      static void Main()
      {
         new class1().Test();
      }

      private void Test()
      {
         Console.WriteLine(this.sample);
      }
    }
 }

// DLL 2

 namespace Assembly2
    {
        public class class2
        {
          // protected string sample = "Test";           
             protected internal string sample = "Test";

       //Same behavior when using protected and protected internal. Why ?

    }
}

I am getting the same behaviour for both the lines.

      // protected string sample = "Test";           
         protected internal string sample = "Test";

But I am sure that there should be some difference.

MSDN says:
Protected: Access is limited to the containing class or types derived from the containing class.

Protected Internal: Access is limited to the current assembly or types derived from the containing class.

I am able to access both protected and protected internal from outside assemblies as long as I inherit from the base class.

Pretty confusing! both looks exactly similar. Can any one explain me by modifying from the above example ?

Was it helpful?

Solution

protected means that you can access the member from any subtype (and of course from the declaring type itself). So regardless of where that subtype is, even if it is in another assembly, you will still have access to all protected members.

internal means that you can access the member from any type in the same assembly. So even a completely unrelated class that lives in the same assembly can access the member.

protected internal combines both, meaning that both apply separately. So you can access the member from any subtype, and you can also access the member from any type in the same assembly.


// Assembly 1
class A {
    protected int foo;
    internal int bar;
    protected internal int baz;
}

class B : A {} // can access: foo, bar, baz
class C {} // can access: bar, baz

// Assembly 2
class D : A {} // can access: foo, baz
class E {} // can access neither

OTHER TIPS

protected internal is protected OR internal, so it behaves exactly as protected from outside the assembly which it's defined, and behaves as internal from inside the same assembly.

Interestingly, the CLR contains a protected AND internal access modifier, which is likely to be included in the next version of C# as private protected - this restricts access only to subtypes within the assembly it is defined.

Anything declared as protected (but not protected internal) is only available from types descending from the type having something protected.

In other words, no other types inside Assembly 2 can access sample, except for those that descend from class2.

If you declare it as protected internal, the above applies, that is, the types that descend from class2 can access sample, but additionally, other types inside the same assembly can access it.

So, protected:

  • Types descending from class2 can access sample, regardless of which assembly this descending type is declared in

and protected internal:

  • Types descending from class2 can access sample, regardless of which assembly this descending type is declared in
  • Other types in assembly2 can access sample as well

This is the analogy I came up with.

Lets assume asembly1 as a country. If the country introduce new 'interal' law, then it is only applicable to the person living on the country. Like traffic laws.

If the country introduce new 'protected' law, then it is applicable to citizen living in or outside the country. Like property, income tax.

protected internal is either protected or internal.

Much thanks to /user/poke

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top