المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How is it possible to access private members of a class becoz the property of the field is made protected internal ?



C# Programming
06-19-2009, 10:37 AM
In the msdn page (http://msdn.microsoft.com/en-us/library/ms173121.aspx)[^ (http://msdn.microsoft.com/en-us/library/ms173121.aspx)]
it says "When a member of a class or struct is a property, field, method, event, or delegate, and that member either is a type or has a type as a parameter or return value, the accessibility of the member cannot be greater than the type. For example, you cannot have a public method M that returns a class C unless C is also public. Likewise, you cannot have a protected property of type A if A is declared as private. "

But in this code
class AccessSpecifier
{
// private field:
private int wheels = 3;

// protected internal property:
protected internal int Wheels
{
get { return wheels; }
set { wheels = value; }
}

}
class Derieved : AccessSpecifier
{
public void fun()
{
Wheels = 90; //accessing private mem of base class
}
}
class main
{
public static void Main()
{
AccessSpecifier a = new AccessSpecifier();
a.Wheels = 4;
Console.WriteLine(a.Wheels); // gives 4 as output
Derieved d = new Derieved();
d.fun();
Console.WriteLine(d.Wheels); //gives 90 as output
d.Wheels = 99; //accessing private mem of a class in same assembly http://www.barakasoft.com/script/Forums/Images/smiley_OMG.gif
Console.WriteLine(d.Wheels);
Console.ReadKey();

}
}

How is this possible to access private members of a class jus becoz the property of the field is made protected internal ? And compiler is against the lines given in MSDN? Can u pls explain ?http://www.barakasoft.com/script/Forums/Images/smiley_confused.gif