2
string a = "John";
string b = "Doe";

a = b; // Shallow copying strings
b = "Elon Musk";

Console.WriteLine(a); // Output: Doe

This prints "Doe", meaning the change in one variable is NOT reflected to the other variable. However, when "boxing in" the string property into a class and performing a shallow copy with class objects, the change in one object is actually reflected to the other object.

public class Person
{
    public string Name { get; set; }
}

// ----

Person person1 = new Person { Name = "John" };
Person person2 = person1; // Shallow copy

Console.WriteLine(person1.Name); // Output: John
Console.WriteLine(person2.Name); // Output: John

person2.Name = "Jane"; // Modifying the property via person2

Console.WriteLine(person1.Name); // Output: Jane (changes are reflected in person1)
Console.WriteLine(person2.Name); // Output: Jane

I am trying to understand what the reason is that the changes are reflected in 2nd case, but not reflected in 1st case. Both class objects and string type are reference-type, therefore, the change should be reflected in the 1st case too, right? Please help me understand what the difference between the two cases that is causing this difference in behavior.

I know that string type is immutable. Are class objects mutable? Is that the reason for the difference between two cases?

Note: I did check previously asked questions, and I could not find a similar question.

4
  • 3
    Person person2 = person1; // Shallow copy <-- This is not a "shallow copy" - that's a reference-copy. A shallow-copy would require a new Person then copying all properties over (though only 1 level-deep, if it were 2+ levels it would be some kind of deep-copy). Commented Jul 19, 2023 at 11:25
  • @Dai thanks for your comment. could you by any chance provide some links where I can learn more about this topic? I couldn't find any proper definitions in C# documentations, and I also couldn't find a definition of reference copying on the internet and how exactly it differs from shallow and deep copying. Maybe the world really needs a medium blog post from one of you guys. This information seems so obscure to me Commented Jul 19, 2023 at 11:59
  • 1
    The terms I used are rather... fundamental in CS+SE, which is why MS's C# documentation doesn't bother to explain them. It would be like asking someone on bicycles.SE to explain what the term "wheel-spoke" means. Best I can do is this: stackoverflow.com/questions/62398990/… - it's for Java, but applies to C# too. Commented Jul 19, 2023 at 12:13
  • @Dai - as a non native speaker I have no idea what "wheel-spoke" is, let me go ahead and ask on bicylces.SE (only kidding - googled the answer) Commented Jul 19, 2023 at 12:51

1 Answer 1

1

string is a reference type (immutable one but still), local variables store reference to some memory occupied by it, even if string was mutable b = "Elon Musk"; does not change the data stored by reference in b it changes the reference itself to a completely new one, i.e. for person sample equivalent would be:

Person person1 = new Person { Name = "John" };
Person person2 = person1;
person2 = new Person { Name = "Elon Musk" }; 

With corresponding effect on the result.

Read more:

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

8 Comments

does that mean when we do b = "Elon Musk", it changes where b variable points to in memory, but does not update where a is already pointing to. And "Doe" in memory does not get cleaned by garbage collection due to still being used by a. And that is why a stays as "Doe"?
@Mephisto yes, exactly. Also why do you think it should affect a?
but then why isn't the same thing happening with class objects? is it because class objects are mutable and can have their data modified in memory?
@Mephisto it happens, update your snippet to the code in answer.
I understand. So it is all about mutability. Now it makes sense. Thanks!
|

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.