如何StringBuilder的容量改变? [英] How does StringBuilder's capacity change?

查看:265
本文介绍了如何StringBuilder的容量改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有一个空的的StringBuilder 容量为5和我写的你好,世界!给它,并在C#标准规定的新产能的StringBuilder ?我有一个模糊的记忆,它的两倍,新的字符串的长度(以避免改变与每一个新的附加字符串的能力)。

When I have an empty StringBuilder with a capacity of 5 and I write "hello, world!" to it, does the C# standard specify the new capacity of the StringBuilder? I have a vague memory that it's twice the new string's length (to avoid changing the capacity with every new appended string).

推荐答案

要看什么版本的.NET的你在说什么。此前.NET 4中,StringBuilder的使用了<一href="http://stackoverflow.com/questions/2247773/is-it-worthwhile-to-initialize-the-collection-size-of-a-listt-if-its-size-reas/2248188#2248188">standard .NET策略时,内部缓冲器每次需要扩大时间的能力增加一倍

Depends what version of .NET you're talking about. Prior to .NET 4, StringBuilder used the standard .NET strategy, doubling the capacity of the internal buffer every time it needs to be enlarged.

的StringBuilder对.NET 4被完全重写,现在使用绳索。延伸的分配,现在是通过将另一块高达8000个字符绳完成。不太一样有效,早期的策略,但避免了与大缓冲区堵塞的大对象堆麻烦。如果你想仔细看看源$ C ​​$ C可从参考源。

StringBuilder was completely rewritten for .NET 4, now using ropes. Extending the allocation is now done by adding another piece of rope of up to 8000 chars. Not quite as efficient as the earlier strategy but avoids trouble with big buffers clogging up the Large Object Heap. Source code is available from the Reference Source if you want to take a closer look.

这篇关于如何StringBuilder的容量改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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