在 .NET 中,为什么 String.Empty
是只读的而不是常量?我只是想知道是否有人知道该决定背后的原因是什么。
最佳答案
使用 static readonly
而不是 const
的原因是与非托管代码一起使用,如 Microsoft 在 Shared Source Common Language Infrastructure 2.0 Release 中所述。 。要查看的文件是 sscli20\clr\src\bcl\system\string.cs
。
The Empty constant holds the empty string value. We need to call the String constructor so that the compiler doesn't mark this as a literal.
Marking this as a literal would mean that it doesn't show up as a field which we can access from native.
我从 this handy article at CodeProject 找到此信息.
关于.net - 为什么 String.Empty 不是常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/507923/