5

Possible Duplicate:
Why are private fields private to the type, not the instance?

Consider the following class:

public class Test
{
    protected string name;
    private DateTime dateTime;

    public Test(string name)
    {
        this.name = name;
        this.dateTime = DateTime.Now;
    }

    public Test(Test otherObject)
    {
        this.name = otherObject.name;
        this.dateTime = otherObject.GetDateTime();
    }

    private DateTime GetDateTime()
    {
        return dateTime;
    }

    public override string ToString()
    {
        return name + ":" + dateTime.Ticks.ToString();
    }
}

In my constructor I'm calling private or protected stuff of the otherObject. Why is this possible? I always thought private was really private (implying only the object could call that method / variable) or protected (only accessible by overloads).

Why and when would I have to use a feature like this?

Is there some OO-logic / principle that I'm missing?

Community
  • 1
  • 1
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203

6 Answers6

5

From MSDN (C# Reference)

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.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
3

To answer your question:

You can use that for example in a Clone method. Where you need write access to members which may be exposed as read only. Like

class MyClass : ICloneable
{
    public object Clone()
    {
        var clone = (MyClass)MemberwiseClone();
        clone.Value = this.Value; // without the way private works, this would not be possible
        return clone;
    }

    public int Value{ get; private set;}
}
dowhilefor
  • 10,971
  • 3
  • 28
  • 45
2

This is behaving exactly as per the C# specification. (It's also how Java determines accessibility.)

Access is applied by the type of express you're accessing, not the identity of the object you're accessing.

As for when it's useful - comparing two objects of the same type, cloning etc... it's handy not to have to expose all your details publicly when you "trust" code in the same class.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

It is private for other classes not for instances of objects and hence since you're in the same class you can access it's private methods.

You can use it for object cloning for example

Jonny
  • 2,787
  • 10
  • 40
  • 62
1

Access modifiers are determined by class, not by object. If you are to access testObject from methods of some other class, same thing would be impossible.

Trivial example for the use of that is the Clone() method, where you have to access other objects private parts to create a copy.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
1

Private means it can only be accessed by code in the same class. That is the case here. You do have another instance, but it's the same class.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210