when we use protected internal any data member of any class then we know that data member can use in same assembly but i did not know that protected internal can be used from other assembly. from here i come to know Have you ever seen design with reasonable usage of protected internal access modifier?
public class A
{
internal protected virtual void YoBusiness()
{
//do something
}
public void test() { }
}
class B
{ // not a derived class - just composites an instance of A
public B()
{
A a = new A();
a.YoBusiness(); // Thanks friend for the access!
}
}
class D : A
{ // derived across assemblies
internal protected override void YoBusiness()
{
// Hey thanks other guy, I can provide a new implementation.
}
}
class C : A
{ // derived across assemblies
public C()
{
YoBusiness();
}
protected override void YoBusiness()
{
// Hey thanks other guy, I can provide a new implementation.
}
}
i knew always that protected internal can be used in same assembly but today know and surprise that any class from other assembly also could override the method......how it is getting possible?
if i want that only the data member can be override or call from same assembly then what i need to do......please discuss. thanks
That's because protected internal
should be read as "protected or internal" (i.e., visible to any class within the same assembly or any class derived from A
regardless of its location), and not as "protected and internal".
From MSDN:
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.
i want that only the data member can be override or call from same assembly then what i need to do
Then just mark it as internal
and virtual
.