为什么 String.Empty 不是常数?

Why isn#39;t String.Empty a constant?(为什么 String.Empty 不是常数?)
本文介绍了为什么 String.Empty 不是常数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET 中,为什么 String.Empty 是只读的而不是常量?我只是想知道是否有人知道该决定背后的原因.

In .NET why is String.Empty read only instead of a constant? I'm just wondering if anyone knows what the reasoning was behind that decision.

推荐答案

之所以使用 static readonly 而不是 const 是因为使用了非托管代码,如微软在 Shared源公共语言基础结构 2.0 版本.要查看的文件是 sscli20clrsrcclsystemstring.cs.

The reason that static readonly is used instead of const is due to use with unmanaged code, as indicated by Microsoft here in the Shared Source Common Language Infrastructure 2.0 Release. The file to look at is sscli20clrsrcclsystemstring.cs.

Empty 常量保存空字符串值.我们需要调用字符串构造函数,以便编译器不会将此标记为字面意思.

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.

我从 CodeProject 上这篇方便的文章中找到了这些信息.

I found this information from this handy article at CodeProject.

这篇关于为什么 String.Empty 不是常数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)