i am reading Accelerated C# 2010. and have a few questions
Question 1
Instances of String are immutable in
the sense that once you create them,
you cannot change them
how is that true. i have not used C# for sometime, plus i am just starting so i maybe wrong even in the syntax.
string str1 = "this is a string"; // i hope my syntax is right
str1 = "this is a NEW string"; // i think i can do this right?
Question 2
If you call the ICloneable.Clone
method on a string, you get an
instance that points to the same
string data as the source. In fact,
ICloneable.Clone simply returns a
reference to this
if this is true, it means that
string str1 = "string 1";
// i hope my syntax is right too. i am really not sure about this
string str2 = str1.Clone();
str2 = "modified string"; // will str1 be modified too?
Sure!
stringis a reference type (a pointer, if you want so see it like this), so in line 2, you are making variablestr1point to a different (constant) string in memory. The original string is not changed, it’s just not referenced any more.No, because you didn’t modify
"string 1". After line 2, it looked like this:After line 3, it looks like this: