栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

StringBuilder类如何实现?每次我们追加时,它在内部创建新的字符串对象吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

StringBuilder类如何实现?每次我们追加时,它在内部创建新的字符串对象吗?

在.NET
2.0中,它在

String
内部使用该类。
String
仅在
System
名称空间之外是不可变的,因此
StringBuilder
可以做到这一点。

在.NET 4.0中

String
已更改为使用
char[]

在2.0中

StringBuilder
看起来像这样

public sealed class StringBuilder : ISerializable{    // Fields    private const string CapacityField = "Capacity";    internal const int DefaultCapacity = 0x10;    internal IntPtr m_currentThread;    internal int m_MaxCapacity;    internal volatile string m_StringValue; // HERE ----------------------    private const string MaxCapacityField = "m_MaxCapacity";    private const string StringValueField = "m_StringValue";    private const string ThreadIDField = "m_currentThread";

但是在4.0中,它看起来像这样:

public sealed class StringBuilder : ISerializable{    // Fields    private const string CapacityField = "Capacity";    internal const int DefaultCapacity = 0x10;    internal char[] m_ChunkChars; // HERE --------------------------------    internal int m_ChunkLength;    internal int m_ChunkOffset;    internal StringBuilder m_ChunkPrevious;    internal int m_MaxCapacity;    private const string MaxCapacityField = "m_MaxCapacity";    internal const int MaxChunkSize = 0x1f40;    private const string StringValueField = "m_StringValue";    private const string ThreadIDField = "m_currentThread";

因此,显然将其从使用a更改

string
为使用a
char[]

编辑:更新了答案以反映.NET 4(我才刚刚发现)中的更改。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/395411.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号