在C#中的字符串和StringBuilder的区别 [英] Difference between string and StringBuilder in c#

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

问题描述

我想知道的字符串的StringBuilder ,也需要理解一些例子。区别

I'd like to know the difference between string and StringBuilder and also need some examples for understanding.

推荐答案

A 字符串实例是不可改变的。在创建后,你不能改变它。出现改变字符串,而不是任何操作返回一个新的实例:

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'S字符串处理函数)。

Immutable objects have some nice properties, such as they can be used across threads without fearing synchronization problems or that you can simply hand out your private backing fields directly without fearing that someone changes objects they shouldn't be changing (see arrays or mutable lists, which often need to be copied before returning them if that's not desired). But when used carelessly they may create severe performance problems (as nearly anything – if you need an example from a language that prides itself on speed of execution then look at C's string manipulation functions).

在您需要的可变的字符串,例如一个你contructing分段或在你改变很多事情,那么你就需要一个的StringBuilder 这是的可以的改变。这具有,在大多数情况下,性能的影响。如果你想有一个可变的字符串,而是与正常字符串实例做,那么你会最终创建和销毁不必要的大量的对象,而的StringBuilder 实例本身将发生变化,否定了很多新对象的需求。

When you need a mutable string, such as one you're contructing piece-wise or where you change lots of things, then you'll need a StringBuilder which is a buffer of characters that can be changed. This has, for the most part, performance implications. If you want a mutable string and instead do it with a normal string instance, then you'll end up with creating and destroying lots of objects unnecessarily, whereas a StringBuilder instance itself will change, negating the need for many new objects.

简单的例子:下面将让很多程序员畏缩痛苦:

Simple example: The following will make many programmers cringe with pain:

string s = string.Empty;
for (i = 0; i < 1000; i++) {
  s += i.ToString() + " ";
}

您最终会在这里创造2001年字符串,2000年被扔掉。同样的例子使用StringBuilder的:

You'll end up creating 2001 strings here, 2000 of which are thrown away. The same example using StringBuilder:

StringBuilder sb = new StringBuilder();
for (i = 0; i < 1000; i++) {
  sb.Append(i);
  sb.Append(' ');
}

这应该放置在内存分配器少得多的压力: - )

This should place much less stress on the memory allocator :-)

不过,应当注意的是,C#编译器是相当聪明的,当涉及到字符串。例如,下面的行

It should be noted however, that the C# compiler is reasonably smart when it comes to strings. For example, the following line

string foo = "abc" + "def" + "efg" + "hij";

将被编译器加入了,只留下在运行时一个字符串。同样地,线,如

will be joined by the compiler, leaving only a single string at runtime. Similarly, lines such as

string foo = a + b + c + d + e + f;

将被改写为

string foo = string.Concat(a, b, c, d, e, f);

,这样你就不必支付五荒谬的串联这将是处理了原始的方法。这不会在循环中保存按照上述(除非编译器展开循环,但我想只有JIT实际上可能这样做,最好不要在该投注)。

so you don't have to pay for five nonsensical concatenations which would be the naïve way of handling that. This won't save you in loops as above (unless the compiler unrolls the loop but I think only the JIT may actually do so and better don't bet on that).

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

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