1

Possible Duplicate:
What is the difference between 'protected' and 'protected internal' ?

I am new to C#, have worked with Java until now.

Now while learning the scope of members in C# I encountered the terms "internal" and "protected internal".

Here I have learned that "internal" is available within the same assembly, which is simillar to "package" level scope equivalent to Java programming.

Now the "protected internal" says: "I am available within the same assembly I'm written in and to those who have inherited me".
So, what is the purpose of “protected internal” scope?

Community
  • 1
  • 1
Mohan Mahajan
  • 1,501
  • 3
  • 12
  • 19
  • This is asking for the purpose, not the difference. I.e. an explanation of how you'd use it and why. IMO not a duplicate, voting to reopen. – jmoreno Jul 12 '21 at 15:55

1 Answers1

3

protected internal means internal OR protected:

  1. The class is available within the same assembly.
  2. The class is available to those who inherit from it from within the assembly or outside it.

normally stacking up modifiers like this would crate a union (meaning protected and internal) but this is not the case this time here.

Teoman Soygul
  • 25,584
  • 6
  • 69
  • 80
  • @Teomal: I would say not OR but AND. – Tigran Jul 18 '11 at 07:29
  • @Tigran, to your surprise, this is exactly an 'protected OR internal' case (which is sort of unique for this sort of modifier stacking). An AND relation would restrict access to only within the same assembly due to 'internal' declaration. – Teoman Soygul Jul 18 '11 at 07:30
  • @Teomal: when I say OR I mean or 1 or 2, when I say And I mean both together. In this case protected internal is internal AND protected. SO can be accessed within assembly AND from derived class ONLY within assembly. – Tigran Jul 18 '11 at 07:33
  • -1. You can not access protected internal member by inheriting it from OUTSIDE of assembly that holds a class. Point 2 is wrong, that what I'm talking about. Just try it in the code. – Tigran Jul 18 '11 at 07:41
  • I made the same test: create a class in Assembly1 with protected internal property, add reference of Assembly1 to Assembly2 and create a public class in Assembly2 that derives a class from Assebly1 with protected property, and I can not access it. And more, just intellicense doesn't show it to me. I use C# 4.0, even if I doubt that things are changed among different versions of Framework. – Tigran Jul 18 '11 at 08:00
  • 1
    @Tigran: But that's wrong. It can be accesses from derived classes within any assembly. – Maximilian Mayerl Jul 18 '11 at 09:19
  • I try it, seriously, and it doesn't work. – Tigran Jul 18 '11 at 09:21
  • 1
    Have a look at that blog post: http://haacked.com/archive/2007/10/29/what-does-protected-internal-mean.aspx – Maximilian Mayerl Jul 18 '11 at 09:23
  • hm.. I did it on property NON virtual one, may be that makes a difference.. BY the way, delete my post as, at this point, I do somethign different from others. Good job. ! – Tigran Jul 18 '11 at 09:26
  • @Teoman: feel free to edit your post so I can put +1. You are right! – Tigran Jul 18 '11 at 09:29