tags:

views:

119

answers:

4

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.

+1  A: 

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.

VexXtreme
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
A: 

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

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

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 ina different assembly).

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

Jason Williams
so you are saying its public in the same assembly and protected in the different assembly
vaibhav
@Jason See edits.
vaibhav
Yes, that's correct.
Jason Williams