4

What is the difference between the "protected" and "protected internal" modifiers in .NET?

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
Bhaskar
  • 4,189
  • 4
  • 26
  • 20
  • possible duplicate of [What is the difference between Public, Private, Protected, and Nothing?](http://stackoverflow.com/questions/614818/what-is-the-difference-between-public-private-protected-and-nothing) – cdeszaq May 18 '11 at 15:36

4 Answers4

10

private

Access is only allowed from within a specific type

protected

private access is expanded to include inheriting types

internal

private access is expanded to include other types in the same assembly

And so it follows that:

protected internal

private access is expanded to allow access for types that either inherit from or are in the same assembly as this type, or both.

Basically, think of everything as private first, and anything else you see as expanding on that.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
7

protected

Members are only visible to inheriting types.

protected internal

Members are only visible to inheriting types and also to all types that are also contained within the same assembly as the declaring type.

Here is a C# example:

class Program
{
    static void Main()
    {
        Foo foo = new Foo();

        // Notice I can call this method here because
        // the Foo type is within the same assembly
        // and the method is marked as "protected internal".
        foo.ProtectedInternalMethod();

        // The line below does not compile because
        // I cannot access a "protected" method.
        foo.ProtectedMethod();
    }
}

class Foo
{
    // This method is only visible to any type 
    // that inherits from "Foo"
    protected void ProtectedMethod() { }

    // This method is visible to any type that inherits
    // from "Foo" as well as all other types compiled in
    // this assembly (notably "Program" above).
    protected internal void ProtectedInternalMethod() { }
}
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
2

As usual, from one of Fabulous Eric Lippert's blog posts:

Many people believe that [protected internal] means "M is accessible to all derived classes that are in this assembly." It does not. It actually means "M is accessible to all derived classes and to all classes in this assembly". That is, it is the less restrictive combination, not the more restrictive combination.

This is counterintuitive to a lot of people. I have been trying to figure out why, and I think I've got it. I think people conceive of internal, protected, and private as restrictions from the "natural" state of public. With that model, protected internal means "apply both the protected restriction and the internal restriction".

That's the wrong way to think about it. Rather, internal, protected and public are weakenings of the "natural" state of private. private is the default in C#; if you want to make something have broader accessibility, you've got to say so. With that model, then it becomes clear that protected internal is a weaker restriction than either alone.

Community
  • 1
  • 1
Lucas
  • 17,277
  • 5
  • 45
  • 40
1

for the difference between protected and protected internal lemme give a brief example and i will match the example ...

Country A: one assembly

Country B: another different assembly

X(Base Class) is dad of Y(Inherited Class) In Country A

Z(Inherited Class of X) is another son of X in Country B.

X has a property.

  1. if X mentions the property as protected then X says: all my sons Y and Z, only you both can access my property wherever u stay...god bless you. No one can access my property except you.

  2. if X mentions the property as protected internal then X says: All the people in my country A including my son Y, can access my property. dear son Z, still you can access my property in Country B.

hope you guys understood...

thank you.

Vamsi
  • 4,237
  • 7
  • 49
  • 74
user497389
  • 11
  • 1