c# 4.0 - Why does the mutable StringBuilder behave like the immutable string when a reference is changed? -
the "c# 4.0 in nutshell" 4th edition book albaharis states on page 249: ". . . calling object.referenceequals guarantees normal referential equality."
so, decided test out.
first tried value types this.
int aa1 = 5; int aa2 = aa1; messagebox.show("object.referenceequals(aa1,aa2) is: " + object.referenceequals(aa1, aa2));
and expected, result false: object.referenceequals(aa1, aa2) is: false
life good. tried mutable reference type this.
system.text.stringbuilder sblr1 = new system.text.stringbuilder(); sblr1.append("aaa"); system.text.stringbuilder sblr2 = sblr1; messagebox.show("object.referenceequals(sblr1,sblr2) is: " + object.referenceequals(sblr1, sblr2));
and expected, result true object.referenceequals(sblr1, sblr2) is: true
life still good. figured since mutable reference type, if change 1 variable null, both should null. tried following.
system.text.stringbuilder sblr1 = new system.text.stringbuilder(); sblr1.append("aaa"); system.text.stringbuilder sblr2 = sblr1; sblr1 = null; messagebox.show("object.referenceequals(sblr1,sblr2) is: " + object.referenceequals(sblr1, sblr2));
and expected them both null. result got false: object.referenceequals(sblr1, sblr2) is: false
now life not good. thought if overrode memory location of sblr1, overriding memory location of sblr2 also.
then thought maybe pointing 2 different nulls, tried this:
system.text.stringbuilder sblr1 = new system.text.stringbuilder(); sblr1.append("aaa"); system.text.stringbuilder sblr2 = sblr1; sblr2 = null; messagebox.show("sblr1 == " + sblr1 + " , sblr2 == " + sblr2);
but here, 1 pointing null this. sblr1 == aaa , sblr2 ==
only 1 null.
it displaying behavior i'd expect immutable reference type string object. string object, can this:
string aa1 = "aax"; string aa2 = "aax"; messagebox.show("object.referenceequals(aa1,aa2) is: " + object.referenceequals(aa1, aa2));
and both reference same thing this. object.referenceequals(aa1, aa2) is: true because "aax" gets written assembly once.
but if this:
string aa1 = "aax"; string aa2 = "aax"; aa1 = null; messagebox.show("after aa1 null(" + aa1 + "), aa2 is: " + aa2);
then point different things this: after aa1 null (), aa2 is: aax
that's because string objects immutable. memory location doesn't overriden. rather, variable points different location in heap memory new value exists. changing aa1 null in above example means aa1 point different location on heap memory.
why mutable reference type behaving same immutable reference type?
edit 4:03pm , 4:08
i've tried this:
system.text.stringbuilder sblr1 = new system.text.stringbuilder(); sblr1.append("aaa"); // sblr1 , sblr2 should both point same location on heap has "aaa". system.text.stringbuilder sblr2 = sblr1; system.text.stringbuilder sblr3 = new system.text.stringbuilder(); sblr3.append("bbb"); sblr1 = sblr3; messagebox.show("sblr1 == " + sblr1 + " , sblr2 == " + sblr2 + " , sblr3 == " + sblr3);
which gave me: sblr1 == bbb , sblr2 == aaa , sblr3 == bbb
that's more result expecting. see now, comments, abscent mindedly expected null act memory location.
i thought if overrode memory location of sblr1, overriding memory location of sblr2 also.
this misunderstanding.
when write this:
system.text.stringbuilder sblr2 = sblr1;
you're assigning sblr2
variable reference same instance of stringbuilder
1 pointed sblr1
. 2 variables point same reference.
you write:
sblr1 = null;
this changes sblr1
variable null reference. didn't change instance in memory @ all.
this has nothing whether reference mutable type or not. you're changing variables, not instance referencing.
as string example:
that's because string objects immutable. memory location doesn't overriden
this not true. fact you're setting 1 string variable null
doesn't have string being immutable. that's separate concern.
why mutable reference type behaving same immutable reference type?
the behavior you're seeing has nothing mutability. standard behavior reference types (whether immutable or mutable). mutability different issue.
the main issue mutability this:
suppose have class, so:
class foo { public int bar { get; set; } }
if write this:
foo = new foo(); a.bar = 42; foo b = a; b.bar = 54; console.writeline(a.bar); // print 54, since you've changed same mutable object
with immutable types, can't happen, since can't change bar
- instead, if make immutable class:
class baz { public baz(int bar) { this.bar = bar; } public int bar { get; private set; } }
you need write:
baz = new baz(42); baz b = a; // isn't legal now: // b.bar = 54; // you'd write: b = new baz(54); // creates new reference
alternatively, make class return new reference on "change" operation, ie:
class baz { public baz(int bar) { this.bar = bar; } public int bar { get; private set; } public baz alter(int newvalue) { return new baz(newvalue); } // may copy other data "this" }
then when you'd write:
baz = new baz(42); baz b = a.alter(54); // b new instance
this happens string
- of methods return new instance, since string immutable, can never "change" existing copy.
Comments
Post a Comment