1

Why does the original string remain unchanged in example 1 and the original instance not get nulled in example 2, when the string property of an object instance is changed in example 3?

    class A { public string property = "class a"; }

    static void Main(string[] args)
    {

        //Example1
        string var = "something";
        string[] array = new string[] { var };
        array[0] += " again";

        // var is unchanged, contents in array slot zero are changed

        //Example2
        A instance = new A();
        object[] array2 = new object[] { instance };
        array2[0] = null;

        // instance is also unchanged, contents in array slot zero are now null

        //Example3
        A anotherInstance = new A();
        object[] array3 = new object[] { anotherInstance };
        (array3[0] as A).property = " else";

        // the *propery* of anotherInstance changes as expected

        return;
    } 

3 Answers 3

3

When you assign a value in an array, you create a copy of the original value. You're not creating a copy of the string itself, but a copy of the value of the "var" variable - which is a string reference. After that point of time, there's no relationship between the variable and the array element - they happen to have the same value immediately afterwards, but they're independent. Changing the array element to make it refer to a different object doesn't change the value of the variable, or vice versa.

In particular, this line:

array[0] += " again";

does not change the contents of the existing string. Instead, it creates a new string and assigns that reference to array[0].

Now in your "Example3" you aren't changing the value of the array element itself - you're changing the contents of the object that both anotherInstance and array3[0] refer to. It's like two people knowing about the same house - if one person paints the door red, the other person will see the red door too.

This all has very little to do with arrays - you'd see the same effects with individual variables too. See my article on reference and value types for further information.

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

2 Comments

Ya know, while this is more correct than any other explanation i've seen so far, it's also rather complicated in relation to what it's explaining. References and variables should be simpler to explain...
thanks... BTW this question came up for me when passing objects into a function using the c# params keyword... for some reason I didn't see the behaviour I should have expected ... Example: void Myfunc(params object[] args)
0

String in .NET are immutable. This means that once they are created/initialized they cannot be changed subsequently. When you do appending to a string a new string is created as opposed to the string being destroyed and reassigned again.

Comments

0

In your first example the first element of the array is a reference to a string. You then create a new string with + and change the first element of the array to be a reference to that new string. var is still a reference to the old string as you did not change var.

In your second example the first element of the array is a reference to an instance of A and again you just change the reference, not the referred-to object.

In your third example, you don't change the reference (you don't reassign array3[0], i.e. you never do array3[0] = something), but you actually change the object that is being referred to. This change also affect anotherInstance because both anotherInstance and array[0] are references to the same object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.