21

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.

ADD

Here is an useful link mentioned by Habib:

Why are private fields private to the type, not the instance?

2

5 Answers 5

33

// this is strange, the msg shouldn't be accessible here.

private members are accessible inside the class, they are not accessible outside the class.

Private - MSDN

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

Sign up to request clarification or add additional context in comments.

8 Comments

I think I need to understand it literally.
the Output method you are calling with obj2.Output() can access the msg variable but you could not access it from the outside with obj2.msg
Yes, but I don't feel you have addressed what he is calling strange. In the F method there is passed a local object of type C, this object instance is only available here because the method is in inside the class C itself...
@Killercam, I added that just before or with your comment in my answer.
@smwikipedia, I believe the confusion is because of two objects but of same type, I have added some extra details, I hope it is more clear now.
|
13

If i understand private correctly, it means access only from within the same class, but not only from the same instance of that class.

Comments

9

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.

Comments

3

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

Comments

1

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";
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.