6

What is the difference between access specifier protected and internal protected in C# ?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user422560
  • 69
  • 1
  • 2
  • possible duplicate of [What does Protected Internal means in .Net](http://stackoverflow.com/questions/2651137/what-does-protected-internal-means-in-net) and [What is the difference between ‘protected’ and ‘protected internal’ ?](http://stackoverflow.com/questions/585859/what-is-the-difference-between-protected-and-protected-internal) – Jesse C. Slicer Aug 19 '10 at 18:58

9 Answers9

10

Internal can be seen within the assembly.

Protected can be seen by classes inheriting from the class where it is defined.

Protected internal can be seen within the assembly OR types derived from the class where it is defined (including types from other assemblies).

See: http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx

Copied from the page:

public              Access is not restricted.
protected           Access is limited to the containing class or types derived from the containing class.
internal            Access is limited to the current assembly.
protected internal  Access is limited to the current assembly or types derived from the containing class.
private             Access is limited to the containing type.
Lasse Espeholt
  • 17,622
  • 5
  • 63
  • 99
  • What's the point of protected internal? Wouldn't protected on it's own do exactly the same thing? –  Aug 25 '10 at 06:27
  • @Vince, No, if it is `protected internal` and you derive from the class in another assembly, you would not have access to the method etc. But if it only was `protected` you would. – Lasse Espeholt Aug 25 '10 at 07:00
  • If I don't have access to the method in the other assembly, then I could simply mark it internal rather than protected internal. Am I missing something here? :) –  Aug 25 '10 at 10:59
  • @Vince Hmm yes, but then you would have access to the method without deriving from it. – Lasse Espeholt Aug 25 '10 at 13:08
  • you said "if it is protected internal and you derive from the class in another assembly, you would not have access to the method", however marking a method as internal in assembly A and deriving from that class in assembly B would give you same result would it not ? –  Aug 30 '10 at 01:04
2

protected means only the current class and any classes deriving from it have access to the member.

internal means any class within the current assembly has access to the member.

protected internal essentially means protected or internal; i.e., all classes deriving from the current class (in any assembly) have access to the member, as do all classes in the current assembly. This is in contrast with what many developers expect -- that protected internal would mean the same thing as protected and internal (it doesn't).

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
2
  • internal - Visible by anything within the same assembly (.dll or .exe).
  • protected - Visible by any sub-classes, no matter where they are.
  • internal protected - Visible by anything within the same assembly and any sub-classes, no matter where they are.

The way Jeff Mattfield says "internal further reduces that visibility" makes it unclear. internal and protected are simply different visibilities. Having both together makes the member more visible. The default visibility of something with no explicit access modifiers, is as small as possible. Adding any access modifiers always increases the visibility.

Rich
  • 3,081
  • 1
  • 22
  • 24
1

internal protected or protected internal which is the same means externally protected (from outside the current assembly) and internally public (from within the same assembly).

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

- Update answer 2019 -

Hi You can find accessibility via below table

enter image description here

Andi AR
  • 2,678
  • 2
  • 23
  • 28
1

Protected internal and protected access specifier relate to the concept of inheritance.

Let us take example to explain protected and protected internal.

There are two namespaces named namespace A and namespace B.

In namespace A, there is a class named classA which consists of a method named accept() using protected access specifier.

In namespace B, there is another class, named classB, which inherits from classA of namespace A.

Now with the help of this protected specifier we can access that accept() method in the classB of namespace B.

But that concept is not true when using the protected internal access specifier: if accept() function of classA of namespace A was using protected internal access specifier, then classB of namespace B cannot access it because that accept() function can only be accessed within the inherited class within the same namespace.

Tony Rad
  • 2,479
  • 20
  • 32
0

To better Understand difference between protected and Protected Internal.it is better to first know what is difference between Protected and internal.

Internal variable refer to same assembly.Yo can't access in different assembly. protected variable like private variable but you can access in drived class in same assembly or diffrent assembly.

 namespace InternalTest   ----This namespace in assembly One
 {
    Public class A
    {
       B ol=new B();
        Console.WriteLine(ol.x);//Output:5  
        Console.WriteLine(ol.y);//error will occured. Because protected is like Private variable
    }

    public class B
     {
        Internal int x=5;
         Protected int y=5;

      }
 }

2) Take diffrent assembly.

     using InternalTest;   
     namespace InternalTest1   ----This namespace in assembly Two
     {
       Public class A1:B
        {
           Public void GetInternalValue()
          {                     return x; //error can't access because this is internal

          }

          Public void GetProtectedValue()
          {
                return y;//Work because it's protected

          }
      }
       public class C
       {


       }

    }

from above example it clears you can access internal in same assembly but not in different assembly.You can say in same assembly it look likes public variable.you can assign value by creating object of class

3)protected internal have goodness of both in same assembly it look like public variable. in diifrent assembly you use like protected variable

sushil pandey
  • 752
  • 10
  • 9
0

internal protected allows you to access members within the same assembly from classes which are not derived from the same object, but also allows the standard protected access you get for accessing the members from another assembly. It's internal | protected, not internal & protected (although the CLR allows the latter, C# does not)

Mark H
  • 13,797
  • 4
  • 31
  • 45
  • From my understanding, protected internal makes no sense, protected internal is the same as protected and allows you to do exactly the same thing –  Aug 30 '10 at 01:06
  • @Vince - It allows the same access as protected, but **also** allows access from **non-derived** classes within the same assembly. (Which is not the same as protected). The difference only matters within the assembly it's used - in other assemblies where you reference that - it makes no difference to the programmer if it's protected or internal protected. – Mark H Aug 30 '10 at 14:06
-1

PROTECTED

If the class is declared as "protected" means it can be accessed by the child class within the assembly as well as child class from outside the assembly.

INTERNAL

If the class is declared as "internal" means it can be accessed by any class within the assembly.

PROTECTED INTERNAL

If the class is declared as "protected internal" means it can be accessed within the assembly by their derived class only.

Im0rtality
  • 3,463
  • 3
  • 31
  • 41
DINESH C
  • 301
  • 1
  • 2
  • 7