How does StringBuilder work internally in C#?

后端 未结 4 414
醉酒成梦
醉酒成梦 2020-11-29 04:50

How does StringBuilder work?

What does it do internally? Does it use unsafe code? And why is it so fast (compared to the + opera

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 05:35

    The Microsoft CLR does do some operations with internal call (not quite the same as unsafe code). The biggest performance benefit over a bunch of + concatenated strings is that it writes to a char[] and doesn't create as many intermediate strings. When you call ToString (), it builds a completed, immutable string from your contents.

提交回复
热议问题