Reputation: 64223
I have this class:
class C
{
private String msg;
public void F(C obj, String arg)
{
obj.msg = arg; // this is strange, the msg shouldn't be accessible here.
}
public void Output()
{
Console.WriteLine(msg);
}
}
The test code is:
C obj1 = new C();
C obj2 = new C();
obj1.F(obj2, "from obj1");
obj2.Output();
The Output is:
from obj1
So, obj2's private member is accessed from another object obj1. I think this is kind of strange.
Here is an useful link mentioned by Habib:
Why are private fields private to the type, not the instance?
Upvotes: 21
Views: 1450
Reputation: 1032
Similar and weirder considering Protected access modifier,
class Base
{
protected String msg;
public void Mtd1(Base baseObj)
{
baseObj.msg = "can access";
}
}
class Child : Base
{
public void Mtd2(Base baseObj, Child childObj)
{
baseObj.msg = "can not access"; //compile time error
childObj.msg = "can access";
}
}
Upvotes: 1
Reputation: 23596
The private
modifier means that it may only be accessed within the same class - not only the same instance.
The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared.
From the C# Library
Upvotes: 3
Reputation: 223277
// this is strange, the msg shouldn't be accessible here.
private
members are accessible inside the class, they are not accessible outside the class.
The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared
For your other question:
So, obj2's private member is accessed from another object obj1. I think this is kind of strange.
You are passing address of obj2
to an instance method of obj1
, and then accessing obj2
's private member msg
in the method and changing its value. But since both of them are of same type, you get the impression that you are accessing other class private member.
Try it with two different classes and you will be able to understand it better.
Consider if you have another class defined as:
class B
{
public void SomeMethod(C obj, string arg)
{
obj.msg = arg; // that is an error.
}
}
now you can't access private member msg
since you are trying to access it outside of the class, in your example, you are accessing the class member inside the class.
There could be an argument that why C# allows instance.PrivateMember
inside the class, the language designers could have restricted the usage of private to this.PrivateMember
, so that the private member is only accessible with the current instance. If that would have been the case then your code would raise the error on obj.msg = arg;
. Apparently the C# designers chosen the private to instance access instead of private to current instance only, so the basic rule is that private
members are accessible inside the class, whether with the this
(current instance) or with an instance of same type. For more discussion why this was done, you can see this question
Upvotes: 33
Reputation: 297
If i understand private correctly, it means access only from within the same class, but not only from the same instance of that class.
Upvotes: 13
Reputation: 12849
Private means it can be accessed from methods of same class, not that you have to access it through this
. It might seem weird, but it is still same class, even though it is different variable and not this
.
Same thing can be done with static methods.
Upvotes: 9