31

Protected Means, we can access this member only in a deriving class, and internal means we can access this member in any type in the same assembly using a object. So can I consider a Protected Internal member as a public member in the same assembly. and as a protected member in the different assembly.

EDIT:

namespace pracConsole
    {
class Class1
{
    protected internal int val;
    public int hello()
    {
        Console.WriteLine("This is method pracConsole.hello");
        Console.ReadLine();
        return 1;

    }
}
class program
{
    static void Main(string[] args)
    {
        Class1 _class1 = new Class1();
        _class1.val = 3;
        _class1.hello();
        Console.ReadLine();
    }
}

}

See I am able to access, protected internal in a non deriving class...so its working as public in same assembly..what do you say.

Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163
  • 1
    +1 Good question. I also ran into this trouble and thought it would mean protected **AND** internal, but it does not, as pointed out by Jason below. – gehho Apr 16 '10 at 07:18

4 Answers4

49

It's a confusing one.

protected means "only this class and derived classes".

internal means "only classes in this assembly".

protected internal means "protected OR internal" (any class in the same assembly, or any derived class - even if it is in a different assembly).

i.e. it does not mean "protected AND internal" (only derived classes within the same assembly).

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • so you are saying its public in the same assembly and protected in the different assembly – Vaibhav Jain Apr 16 '10 at 07:05
  • Wow, its so easy as a beginner programmer to not understand the subtle difference between the and and the or. – Aelphaeis Mar 27 '14 at 21:06
  • 1
    Wow that's really wierd, I was sure it would be AND. – MrFox Apr 24 '14 at 12:24
  • However you can also expose Internals to other trusted libraries using the [`InternalsVisibleToAttribute`](http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx) – Shaun Wilde Oct 30 '14 at 00:01
  • You could, but that is really just an ugly bodge to support the poorly designed unit testing approach employed by VS, and I'd recommend stayjng well away from using it in production code. (it's essentially "friend", and just breaks encapsulation) – Jason Williams Oct 31 '14 at 20:12
2

Internal means that only classes within the same assembly can access that member

Protected means the member can only be accessed by a deriving type (child class accessing a super class).

Protected internal is a combonation of both of them. It can only be accessed within the same assembly and it can only be accessed as a child class.

More simply: 'protected internal' means 'protected or internal' - this means that it can be accessed within the same assembly or by a deriving type.

MAS1
  • 1,691
  • 6
  • 19
  • 22
  • I understand these things, but I want to know , can I safely say its public in the same assembly and protected in the different assembly – Vaibhav Jain Apr 16 '10 at 07:11
  • You contradict yourself: "It can only be accessed within the same assembly *and* it can only be accessed as a child class." vs. "this means that it can be accessed within the same assembly *or* by a deriving type". At least that's my understanding of your post. Maybe you should remove the second sentence in the third paragraph starting "It can only be accessed...". – gehho Apr 16 '10 at 07:15
1

Protected internal means that only derived types and types in the same assembly can access the member. It's strange, but it's a union relationship. Meaning, the member can be accessed by anything that can access members marked as internal OR protected.

  • So give it a thought, If I am able to access a protected internal via a object in the same assembly, then isn't it working as a Public. – Vaibhav Jain Apr 16 '10 at 06:58
-4

Not really. The Protected keyword in the declaration statement specifies that the elements can be accessed only from within the same class, or from a class derived from this class. So you can access it from the same library but not from all classes.

And you cannot access Protected Internal from any other library because Internal means access only from the same assembly.

Hun1Ahpu
  • 3,315
  • 3
  • 28
  • 34
  • Wrong answer. The question has been answered correctly by other posters, though: internal in its assembly, protected outside it. And it is not "library" in .NET, but "assembly". – Gorpik Apr 16 '10 at 07:43