What is the difference between access specifier protected
and internal protected
in C# ?
views:
135answers:
5Internal
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.
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)
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).
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).
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.