开发者

Questions on C# Strings: Immutablility & Cloning

i am reading Accelerated C# 2010. and have a few questions

Question 1

Instances of String are immutable in the se开发者_StackOverflownse 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? 


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? 

Sure! string is a reference type (a pointer, if you want so see it like this), so in line 2, you are making variable str1 point to a different (constant) string in memory. The original string is not changed, it's just not referenced any more.

string str1 = "string 1";
string str2 = str1.Clone(); // i hope my syntax is right too. i am really not sure about this
str2 = "modified string"; // will str1 be modified too? 

No, because you didn't modify "string 1". After line 2, it looked like this:

memory            "string 1"
                    ^    ^ 
                    |    |
stack             str1  str2

After line 3, it looks like this:

memory            "string 1"     "modified string"
                      ^              ^
                      |              |
stack                str1           str2


The consensus answer here is correct.

The string type is a reference type to an immutable string in memory. A collection of all your strings is created in memory (aptly referred to as a string pool) that contain the strings you have created. In question 1, your first statement creates a string in memory, and assigns str1 a reference to that string in the string pool.

When you do the following

str1 = "this is a NEW string";

you are actually adding a new string to the string pool (creating a new string transparently) then assigning str1 a reference to the new string.

Consider this, for example:

string str1 = "this is my old string";
str1 = "this is my NEW string"; 
str1 = "this is my old string";

In this scenario, only two strings are created in the string pool. When you reach the third line in your code, the .NET platform checks the string pool, finds a match, and gives str1 a reference to the string in the string pool that already exists, rather than creating a duplicate.

To drive the point home, let's do this:

string str1 = "this is a string";
string str2 = "this is a string";
//both strings have a reference to the same string in memory
str1 = str1.Replace("this","xxxx");

Think about what's happening here. str1 and str2 have a reference to the same string in the string pool. str1.Replace() is not actually changing the existing string in the string pool(if it did, str1 AND str2 would have the new value), but rather it is creating a new string and then assigning str1 a reference to it.

Strings are immutable in the sense that, once they exist in memory, they cannot be changed. The StringBuilder in C# allows us to avoid this problem. We can freely modify what is in a StringBuilder object. The string we create isn't commited to the string pool until we invoke the .ToString() method.


You are referring to the references to the string. Those are indeed mutable. Remember, strings are reference types.

That's why you can change string variable assignments.

However, actually trying to change the contents of the string is difficult, to say the least (it's not impossible, but for all intents and purposes, should be considered to be so).

This is why all the methods on strings which perform operations on the string (Trim, ToUpper, etc, etc) return new strings; the original one the method was called on is immutable.


Question 1: Correct, if that's not the desired functionality, look into a StringBuilder object.

Question 2: No, the clone returns a reference to the original string. Any modifications will be done to the memory referenced by the new pointer str2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜