tags:

views:

1492

answers:

4

Hi All, Can someone please elaborate me the difference between 'protected' and 'protected internal' modifiers in C#? It looks they behave in same manner.

+21  A: 

From MSDN (click for more information):

protected:

The type or member can only be accessed by code in the same class or struct, or in a derived class.

internal:

The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal:

The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.

M4N
+6  A: 

"protected" can be used by any subclasses from any assembly.

"protected internal" is everything "protected" is, plus also anything in the same assembly can access it.

Importantly, it doesn't mean "subclasses in the same assembly" - it is the union of the two, not the intersection.

Marc Gravell
+1  A: 

In practice, about methods:

protected - accessible for inherited classes, otherwise private

internal - public only inside it's class assembly, otherwise private

protected internal - means protected or internal - methods becomes accessible for inherited classes and for anybody from it's assembly

abatishchev
I would use OR to express that cause it is either not both that has to be true.
Brian Rasmussen
I do not completely agree with the part "for changing base class behavior" in the description of "protected". I'd say this is where you use "virtual" (on the base class) and "override" (on the derived class).
M4N
Agree. Sorry, my mistake. Have edited
abatishchev
A: 

protected: the varible or method will be avalible only to the child class in any assembly protected internal:it will be available to child classes of any assembly and to all the classes within the same assembly