How is it possible to access private members of a class becoz the property of the field is made protected internal ?
-
In the msdn page [^] 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 :omg: 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 ?:confused:
-
In the msdn page [^] 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 :omg: 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 ?:confused:
"only derived types or types within the same assembly can access that member." The key word is 'OR'. You are in the same assembly.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
In the msdn page [^] 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 :omg: 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 ?:confused:
You are accessing the property Wheels which is protected, and not the private field wheels. Nothing odd with that; indeed, that is the reason you use properties.
Cheers, Vikram.
Recent activities: TV series: Friends, season 8 Books: Freakonomics, by Steven Levitt and Stephen J Dubner.
Carpe Diem.
-
You are accessing the property Wheels which is protected, and not the private field wheels. Nothing odd with that; indeed, that is the reason you use properties.
Cheers, Vikram.
Recent activities: TV series: Friends, season 8 Books: Freakonomics, by Steven Levitt and Stephen J Dubner.
Carpe Diem.
Actually, protected means he should not be able to see it. Protected internal, means he does, because it's internal.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
-
Actually, protected means he should not be able to see it. Protected internal, means he does, because it's internal.
Christian Graus Driven to the arms of OSX by Vista. "! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums. I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
I was talking about him using it in the Derived class. You are right in that internal alone would work, but so would protected alone.
Cheers, Vikram.
Recent activities: TV series: Friends, season 8 Books: Freakonomics, by Steven Levitt and Stephen J Dubner.
Carpe Diem.
-
In the msdn page [^] 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 :omg: 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 ?:confused:
The statements you quote refer to the type of the class member. A property in class Y can't return a value of type X if type X is private to class Y.
public class Y
{
private class X { ... }public X P { ... } // Not allowed
}