Reputation: 303
string name = "bob";
object obj = name;
obj = "joe";
Console.WriteLine(name);
I'm a bit confused on how name will print bob. If string and object are both reference types, shouldn't they point to the same piece of memory on the heap after the "obj = name" assignment? Thanks for any clarification.
Edit: StackUnderflow's example brought up another related question.
MyClass myClass1 = new MyClass();
MyClass myClass2;
myClass1.value = 4;
myClass2 = myClass1;
myClass2.value = 5; // myClass1 and myClass2 both have 5 for value
What happens when both are class references? Why does it not work the same way, as I can change a class field via one reference and it is reflected in the other. Class variables should also be references. Is that where stings being immutable comes into play?
Upvotes: 3
Views: 1149
Reputation: 20314
(Since the OP stated C++ background):
References in C# are not the same as references in C++, they act a bit more like pointers. In C#, they are not "bound" at initialization, so the assignment operator (=
) will assign a new reference instead of modifying the original object.
In C++:
std::string name = "bob";
std::string& obj = name; // now bound to 'name'
obj = "joe"; // name is directly affected
std::cout << name; // prints "joe"
In C#:
string name = "bob";
string obj = name; // both reference "bob"
obj = "joe"; // obj now references "joe", and name still references "bob"
Console.WriteLine(name); // prints "bob"
Upvotes: 5
Reputation: 3523
There is a difference between assignment and acting on a variable. If you had this instead:
public class A { public string Name; }
...
var orig = new A { Name = "bob" };
var obj = orig;
obj.Name = "joe";
Console.WriteLine(orig.Name); //"joe"
Here you would be modifying the object that obj
points to (instead of modifying what obj
points to).
Upvotes: 1
Reputation: 564363
shouldn't they point to the same piece of memory on the heap after the "obj = name" assignment
Yes. They both reference the same string instance. You can see this by calling:
bool sameStringReference = object.ReferenceEquals(obj, name); // Will be true
However, as soon as you do:
obj = "joe";
You're changing this so that obj
is now referencing a different string. name
will still be referencing the original string, however, as you haven't reassigned it.
If you then do:
sameStringReference = object.ReferenceEquals(obj, name); // Will be false
This will be false at this point.
Upvotes: 6
Reputation: 23198
Nope, you're simply reassigning the obj
reference to a "joe" string object. Strings are immutable so they can't be changed that way. Best you can do is wrap name
with your own class.
public class MyName
{
public string Name { get; set; }
}
MyName firstName = new MyName() { Name = "bob" };
MyName secondName = name;
secondName.Name = "joe";
Console.WriteLine(firstName.Name) //outputs "joe"
Upvotes: 1