c# - What role does string immutability play in the explaination of string references to developers? -
in 1 form or encounter following question (posed, here, in pseudo-code):
string mystring = "hello" someobject.stringproperty = mystring mystring = "world"
why doesn't someobject.stringproperty equal "world"?
there seems confusion role following statement plays in explanation of why case:
strings immutable
what think?
if think statement doesn't apply, i'd ask this: in language strings were mutable, , assignment operator mutated actual value (instead of changing reference), answer still make sense?
edit:
ok, feel need clarify things:
i'm not confused how strings, references, or assignments work. i'm clear on topic. i'm not asking how strings work. i'm asking "what role string immutability play in explaination of string references developers". can skip ad-hominem attacks must confused.
i'm looking logically rigorous answer developer asking cited question doesn't contain, or pre-suppose, immutability of strings.
a categorization of existing arguments:
string immutability has nothing because references changing, not values of strings
answer pre-supposes exact fact i'm asking about.assignment means assignment of references not assignment of values
again, pre-supposes exact fact i'm asking about. there no reason must case strings. is case strings performance , other reasons.
the role played statement in explanation depends on explanation itself. harmful or useful, depending on rest of explanation.
personally wouldn't use statement until late in explanation (at least these days). immutability of strings gets in way - parameter passing explanations. i'd start explanation using mutable class, this:
house x = new house(color.green); // has green front door house y = x; x = new house(color.red); console.writeline(y.frontdoorcolor); // still green!
here explain x
, y
pieces of paper addresses of houses on. assignment in second line doesn't copy house - copies address of house. assignment on third line doesn't change color of front door on first house - creates new house, rubs out address on first piece of paper (x
), , writes new address on. doesn't change first house, or second piece of paper (y
).
i'd produce second example:
house x = new house(color.green); // has green front door house y = x; x.frontdoorcolor = color.red; // repainting front door console.writeline(y.frontdoorcolor); // red!
this time there's 1 house - if paint door of house , come see address i'd given earlier, you'll see front door red.
so far, good. now go original example , looks first house snippet rather second one, behaves same way. can string immutability means can't write code looks second house example using strings. string immutability wouldn't have been immediately relevant explanation of existing code, still have appeared in same answer.
(i'd have point out although references behave real-world addresses, i'm not claiming they're same "memory addresses" or pointers. they don't have be. i'm using term in strict analogy real world, , that's all. it's downside of example, don't think harm.)
i might then talk value types, , consider have happened if house
had been value type - discouraging mutable value types.
to know whether or not answer still relevant in language mutable strings, we'd need know more how string literals behaved. language same c# in every way other mutability of strings awful language, write:
// hypothetical bad language string x = "dog"; x.mutateto("cat"); console.writeline("dog"); // prints cat!
that wouldn't desirable, presumably behaviour of string literals have change. talk possible changes meaning of assignment operator, etc... it's hard make concrete statements hypothetical language without knowing how behaves.
Comments
Post a Comment