关于c#:String.Empty不是Const的特定原因吗?

Is there a specific reason that String.Empty is not a Const?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Why isn't String.Empty a constant?

我想在一些属性的属性中使用string.Empty,但后来发现它实际上不是常量,而是静态成员。

微软有什么理由这么做吗?


我想说,在引用的程序集中使用const总是一个非常糟糕的主意。

原因是C编译器将常量视为值而不是引用,正如我在回答中所说的。

With this I mean that the C# compiler will replace all instances of the constant in you code and replace the"variable" with the value.

This means that even if you update the assembly GlobalConstants.dll and copy it to one of the applications you have, you will need to recompile that application. Not doing so, will cause the application to use the old constant values.

To overcome this problem, you can simply use public static readonly instead of public const as the readonly modifier differs from the const in that it is treated by the C# compiler as a reference in code rather than a value.


我认为原因是:字符串是引用类型,而不是值类型。比较两个引用(使用静态成员时)比比较两个字符串实例(使用const时)更快。