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?

Community
  • 1
  • 1
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
  • Nice isn't it? I use that a lot in my "equals" method overriding in Java. – Andreas Tasoulas Feb 15 '14 at 06:53
  • possible duplicate of [Why are private fields private to the type, not the instance?](http://stackoverflow.com/questions/6983553/why-are-private-fields-private-to-the-type-not-the-instance) – nawfal Jul 23 '14 at 08:53

5 Answers5

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

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I think I need to understand it *literally*. – smwikipedia Feb 14 '14 at 14:05
  • 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 – Sandman Feb 14 '14 at 14:07
  • 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... – MoonKnight Feb 14 '14 at 14:08
  • 1
    @Killercam, I added that just before or with your comment in my answer. – Habib Feb 14 '14 at 14:10
  • 1
    @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. – Habib Feb 14 '14 at 14:18
  • @LoïcFaure-Lacroix, there is no friend class in C#, There is an [access specifier closer to friend](http://stackoverflow.com/a/204744/961113) – Habib Feb 14 '14 at 17:14
  • ahh I didn't see the C# tag – Loïc Faure-Lacroix Feb 14 '14 at 20:09
  • Thanks for the detailed explanation. I add your last link to my question, too. – smwikipedia Feb 16 '14 at 10:34
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.

Sascha Rambeaud
  • 297
  • 1
  • 8
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.

Euphoric
  • 12,645
  • 1
  • 30
  • 44
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

Community
  • 1
  • 1
Jojodmo
  • 23,357
  • 13
  • 65
  • 107
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";
    }
}
SajithK
  • 1,014
  • 12
  • 23