0

I have a private setter on a class property of a mutable type, that i set through the classes constructor using myMutableVariable.

However when I change something on myMutableVariableafter Class.Property = myMutableVariable, there is a difference between myMutableVariableand Class.Property.

Changin, for instance is - setting myVariable to null.

The property remains private - does not change. This is true even for lists and its members inside the same private property.

How does that work?

public class Class1
{
    public Class1(string name)
    {
        this._name = name;
    }

    private string _name = "";
    public string Name { get { return _name; } set { _name = value; } }
}

public class Class2
{
    public Class2(Class1 c1)
    {
        this._c1 = c1;
    }

    private Class1 _c1;
    public Class1 C1 { get { return _c1; } }
}

The test:

static void Main(string[] args)
{
    Class1 myMutableVariable = new Class1("c1name");


    Class2 c2 = new Class2(myMutableVariable);
    myMutableVariable = null;

    Console.WriteLine(c2.C1.Name);
    //c2.C1 remains 'c1name'
}
JJ_Jason
  • 349
  • 8
  • 24

3 Answers3

5

All parameters for methods and constructors are, by default, passed by value, not by reference. The Class2 constructor is copying the reference to Class1 that is passed into it. The value of the myMutableVariable variable is a reference to an object. That reference is copied to the new Class2 instance. c2 now no longer cares what happens to the myMutableVariable variable, it only cares about what happens to the object that the reference refers to.

Had you modified the object that myMutableVariable refers to rather than the variable itself then you would see it reflected in the c2 instance. For exmaple, myMutableVariable.Name = "new name"; would result in a different name being printed.

This is the heart of the difference between passing a reference by value versus a value by reference.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • Actually, this answers my original question: http://stackoverflow.com/questions/681287/how-to-make-a-reference-type-property-readonly – JJ_Jason Oct 18 '13 at 23:54
1

I'm not sure if this is your issue, until you clear up your question. However...

If myVariable is a value type and not a reference type then when you are assigning it, the actual value data is stored within its own memory allocation. When you change the value of the private variable it does not change the value of the public property as it has its own memory allocation.

If myVariable is a reference type you are assigning a pointer to another memory location that holds the data. Then if you change the object of the private variable you will see the change also reflected in the public property as it still points to the same object.

Harrison
  • 3,843
  • 7
  • 22
  • 49
  • `you will see the change also reflected in the PRIVATE property` How would I avoid such a scenario? – JJ_Jason Oct 18 '13 at 23:25
0

private just means only the containing class and its members can access the code. so Class.Property can only be set (the reference to the object if it is an object) within the class.

clcto
  • 9,530
  • 20
  • 42