C#中Mutable,Immutable字符串之间的区别 [英] Difference Between Mutable , Immutable string in C#

查看:202
本文介绍了C#中Mutable,Immutable字符串之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

解释C#中可变字符串和不可变字符串。我何时使用字符串构建器而不是字符串,在Live项目中使用bcoz处理String数据类型中的每个字符串/文本。









问候,

Saran.t

解决方案

您应该这样做: GS结果 [< a href =http://www.google.co.in/search?q=String+Vs+Stringbuilder&rls=com.microsoft:en-gb&ie=UTF-8&oe=UTF-8&startIndex= & startPage = 1& redir_esc =& ei = CxhWTYOeFcLyrQeGrZ3cBwtarget =_ blanktitle =New Window> ^ ],并获得这些链接等等:

String Vs StringBuilder(C#) [ ^ ]

StringBuilder vs. String / Fast String Operations with .NET 2.0 [ ^ ]

Performance Titbitz [ ^ ]

Larry的软件工程规则,第1部分 [ ^ ]

所有.NET stings都是 immutable

你根本不能修改任何字符串。所有字符串操作都会创建另一个字符串。

例如,如果你有字符串 S1 S2 S3 = S1 + S2 是一个新字符串,而 S1 S2 保持不变。



与此相关,想象一个循环添加到某些字符串 S (最初 String.Empty )最后的部分越来越多 - 这是一项非常典型的任务。由于初始字符串 S 从未被修改,实际上, S 的旧值与新的部分连接在一起。结束并复制回 S 。通过这种方式, S 被分配给不断增长的新字符串,并且每次都复制所有数据。



相比之下 System.Text.StringBuilder.Append 更有效地做了这件事,因为从来没有再次复制已有的数据, StringBuilder的实例改为改变。如果只使用+操作添加两个字符串,则无关紧要。

字符串操作与 StringBuilder 操作的比较相同。



根据经验,如果您有任何重复操作来组成字符串,请始终使用 StringBuilder 。基本上,在一个表达式中使用多个字符串+并不是很有效; string.Format 功能更好,更快,更易读。



-SA


字符串实例是不可变的。创建后无法更改它。任何看似改变字符串的操作都会返回一个新实例:



string foo =" Foo" ;;

//返回一个新的字符串实例而不是更改旧的字符串实例

string bar = foo.Replace('o','a');

string baz = foo +" bar" ; //同上这里


Hi Guys,
Explain about mutable strings and immutable in C#. When will i use the String builder rather than Strings, bcoz in Live project i handled every strings/Text in String Datatype.




Regards,
Saran.t

解决方案

You should have done this: GS Result[^], and got links like these and many more:
String Vs StringBuilder (C#)[^]
StringBuilder vs. String / Fast String Operations with .NET 2.0[^]
Performance Titbitz[^]
Larry's rules of software engineering, part 1[^]


All .NET stings are immutable.
You cannot modify any string at all. All string operations create yet another string.
For example, if you have strings S1 and S2, S3 = S1 + S2 is a new string, while S1 and S2 remain the same.

In connection to that, imagine a loop adding to some string S (initially String.Empty) more and more portions at the end — a very typical task. As initial string S is never modified, actually, the the old value of S is concatenated with the new portion at the end and copied back to S. In this way, S is being assigned to a growing new string and all the data is copied every time.

In contrast System.Text.StringBuilder.Append does this thing much more effectively, because already available data is never copied again, the instance of StringBuilder mutates instead. If just two strings are added using "+" operations, it does not matter.
Same thing about comparison of string operations with StringBuilder operations.

As a rule of thumb, if you have any repeated operation to compose a string, always use StringBuilder. Basically, using more than one string "+" in one expression is not very effective; string.Format function is much better, faster and more readable.

—SA


A string instance is immutable. You cannot change it after it was created. Any operation that appears to change the string instead returns a new instance:

string foo = "Foo";
// returns a new string instance instead of changing the old one
string bar = foo.Replace('o', 'a');
string baz = foo + "bar"; // ditto here


这篇关于C#中Mutable,Immutable字符串之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆