c# - 为什么 C# 的 String.Join 不在某一时刻使用 StringBuilder?

标签 c# stringbuilder .net-standard standard-library

<分区>

可以看出hereString.Join 的重载之一使用原始指针并使用称为 UnSafeCharBuffer 的东西。为什么是这样?是性能优化吗?

最佳答案

Is a performance optimization?

是的。

一般来说,您应该预料到不安全代码要么用于低级非托管语言互操作,要么用于性能优化。在这种情况下是后者。

然后这提出了问题:

Why not use the same techniques for StringBuilder?

可以使用不同的优化技术来调整不同的场景; StringBuilder 针对其场景进行了优化。

场景在几个方面是不同的。 Join 提前知道将返回多少字节; StringBuilder 没有。 Join 知道生成的字符串恰好一次,但是 StringBuilder 必须支持创建、追加、ToString, append, ToString, ... 高效的工作流程。等等。

关于c# - 为什么 C# 的 String.Join 不在某一时刻使用 StringBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54244620/

相关文章:

c# - 无法在 Rider 中使用 .NET Standard 2.0 Lib 加载 Xamarin Forms 项目

c# - c# 中在 mysql 字符串中间创建换行符的正确语法是什么?

.net-core - 如何正确引用 NETStandard 2.1?

c# - 分组而不复制供应商行

java - StringBuilder 每次都将内容追加到新行

Java new String 和 new StringBuilder 在堆中的行为

C# 字符串连接最佳实践

c# - .netstandard 库和 UniversalApiContract

c# - public static HashSet<string> 线程安全吗?

c# - 使用标记的 GridView 行号(仅限)