views:

135

answers:

5

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

+9  A: 

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.
lasseespeholt
What's the point of protected internal? Wouldn't protected on it's own do exactly the same thing?
Vince
@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.
lasseespeholt
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? :)
Vince
@Vince Hmm yes, but then you would have access to the method without deriving from it.
lasseespeholt
@lasseespeholt 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 ?
Vince
A: 

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
See http://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx.
Uh, what am I looking for?
Mark H
From my understanding, protected internal makes no sense, protected internal is the same as protected and allows you to do exactly the same thing
Vince
@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
Ok I get it now
Vince
+1  A: 

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
+2  A: 

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
+2  A: 
  • 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