G
Guest
It seems odd that a private instance member can be accessed by any other
instance of its defining class. For example:
class A
{
private int x = 1;
public void func (A a)
{
Console.Out.WriteLine ( "private member = {0}", a.x);
}
}
Rather than limit 'private' to an instance, C# allows any instance of A to
have access to another instance's private members - as in the above code.
This would be like allowing any student in an 8th grade math class access to
the exam answers written by any other student. It seems like 'private'
should only allow 'this.x' - not 'a.x' access.
If someone can explain this to me I'd be grateful.
instance of its defining class. For example:
class A
{
private int x = 1;
public void func (A a)
{
Console.Out.WriteLine ( "private member = {0}", a.x);
}
}
Rather than limit 'private' to an instance, C# allows any instance of A to
have access to another instance's private members - as in the above code.
This would be like allowing any student in an 8th grade math class access to
the exam answers written by any other student. It seems like 'private'
should only allow 'this.x' - not 'a.x' access.
If someone can explain this to me I'd be grateful.