可变字符串和不可变字符串 [英] mutable string and immutable string
本文介绍了可变字符串和不可变字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
mutable和immutable之间有什么区别
what is difference between mutable and immutable
推荐答案
在C#中,所有字符串都是immutable
- 这是一种奇特的说法不能改变(mutable
意味着它可以改变)。
看起来好像你是更改字符串但每次对不可变对象执行操作时,它都会发生在字符串的副本中并返回一个新字符串。
例如:
In C#, all strings areimmutable
- which is a fancy way of saying "can''t be changed" (mutable
means it can be changed).
It may look as if you are changing a string but every time you perform an operation on a immutable object, it happens to a copy of the the string and a new string is returned.
For example:
string s1 = "hello ";
string s2 = s1 + "there ";
string s3 = s2 + "Joe!";
Console.WriteLine(s1);
Console.WriteLine(s2);
Console.WriteLine(s3);
因为字符串是不可变的,所以创建s2不会影响s1,类似地创建s3不会影响s2或s1:
Because strings are immutable, creating s2 does not affect s1, and similarly creating s3 does not affect s2 or s1:
hello
hello there
hello there Joe!
一旦你创建了一个字符串类的成员,它就永远不会改变。另一方面,StringBuilder是可变的 - 您可以添加和删除数据,只有在分配的空间太小而无法保存新数据时才会复制它。
Once you create a member of the string class, it will never be changed. A StringBuilder on the other hand is mutable - you can add and remove data from it, and it is only ever copied when the space allocated is too small to hold the new data.
看看这里:
[ ^ ]
http://stackoverflow.com/questions/4274193/what-is-the-difference-between-a -mutable-and-immutable-string-in-c [ ^ ]
http://stackoverflow.com/questions/3811016/what-is-the-difference-between-mutable-and -immutable [ ^ ]
http:// www。 dotnetspider.com/forum/116582-What-mutable-immutable.aspx [ ^ ]
http://www.netsqlinterviewquestions.com/csharp_interview_questions/338_what-is-mutable-immutable-what-is-the-difference-between-string-and -stringbuilder.aspx [ ^ ]
http:// social。 msdn.microsoft.com/Forums/br/csharpgeneral/thread/8b79b0f6-c404-4ed3-a730-586f1fcd5fa9 [ ^ ]
http://stackoverflow.com/questions/7088108/in-c-sharp-are-value-types-mutable - 或者不可变 [ ^ ]
Hi,
Have a look here:
Difference Between Mutable , Immutable string in C#[^]
http://stackoverflow.com/questions/4274193/what-is-the-difference-between-a-mutable-and-immutable-string-in-c[^]
http://stackoverflow.com/questions/3811016/what-is-the-difference-between-mutable-and-immutable[^]
http://www.dotnetspider.com/forum/116582-What-mutable-immutable.aspx[^]
http://www.netsqlinterviewquestions.com/csharp_interview_questions/338_what-is-mutable-immutable-what-is-the-difference-between-string-and-stringbuilder.aspx[^]
http://social.msdn.microsoft.com/Forums/br/csharpgeneral/thread/8b79b0f6-c404-4ed3-a730-586f1fcd5fa9[^]
http://stackoverflow.com/questions/7088108/in-c-sharp-are-value-types-mutable-or-immutable[^]
这篇关于可变字符串和不可变字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文