.net - 为什么 String.Empty 不是常量?

标签 .net string readonly constants

在 .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/

相关文章:

.net - project.json 相当于 InternalsVisibleTo

php - 如何在 PHP 中获取字符串的字节值?

android - 如何实现只读的ContentProvider?

ubuntu - 在 Linux Snappy Ubuntu Core 上更改密码

c# - EF Core 2.1.4 FileLoadException System.ComponentModel.Annotations 4.2.0.0

c# - 创建一个包含大量文件的虚拟文件夹

c# - 如何避免在加密和解密过程中使用额外的内存?

javascript - 如何在 JavaScript 中用空字符串替换 'undefined'

python - 将字符串转换为树

python - 在 ModelFormSet 中包含 editable=false 字段