我为什么要使用 StringBuilder简单地附加字符串?例如为什么要像这样实现:
StringBuilder sb = new StringBuilder;
sb.Append("A string");
sb.Append("Another string");
结束
String first = "A string";
first += "Another string";
?
最佳答案
StringBuilder 的文档解释其目的:
The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.
关于c# - StringBuilder 解决了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5498868/