gpt4 book ai didi

c# - 为什么 string.Empty 比 ""更值得推荐?

转载 作者:行者123 更新时间:2023-11-30 13:20:40 24 4
gpt4 key购买 nike

为什么 string.Empty"" 更值得推荐?

是不是因为当编译器在解析代码的时候,来了一个",编译器会准备读取一个字符串?但是在string.Empty中,编译器会甚至没有准备好读取字符串?

最佳答案

还有一个原因。

常量,由于其性质,是一个 静态是对某些应用程序域中所有线程共享的单个实例的引用,而文字最终会产生 N空字符串的实例。

这就是为什么建议使用 string.Empty constant 只读字段而不是使用空的 "" 字符串文字,显然,作为其他人说,它增加了可读性。

无论如何,string interning 应该被考虑在内,因为在某些情况下,可能会发生两个或更多包含相同字符串的文字可能最终出现在一个实例中(see remarks section on String.IsInterned docs):

The common language runtime automatically maintains a table, called the intern pool, which contains a single instance of each unique literal string constant declared in a program, as well as any unique instance of String you add programmatically by calling the Intern method.

关于c# - 为什么 string.Empty 比 ""更值得推荐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5650164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com