Is there a specific reason that String.Empty is not a Const?
Possible Duplicate:
Why isn't String.Empty a constant?
我想在一些属性的属性中使用
微软有什么理由这么做吗?
我想说,在引用的程序集中使用
原因是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时)更快。