重要的部分不是在这个类中发生了什么,而是当另一个类使用(并链接到)它时会发生什么。让我用另一个例子来解释:
假设您有一个 Assembly1.dll 包含一个声明的类
public static const int SOME_ERROR_CODE=0x10;
public static readonly int SOME_OTHER_ERROR_CODE=0x20;
和另一个消耗这个的类,例如
public int TryFoo() {
try {foo();}
catch (InvalidParameterException) {return SOME_ERROR_CODE;}
catch (Exception) { return SOME_OTHER_ERROR_CODE;}
return 0x00;
}
您将您的类编译成 Assembly2.dll 并将其链接到 Assembly1.dll,正如预期的那样,您的方法将在无效参数上返回 0x10,在其他错误上返回 0x20,在成功时返回 0x00。
特别是,如果您创建的 Assembly3.exe 包含类似
int errorcode=TryFoo();
if (errorcode==SOME_ERROR_CODE) bar();
else if (errorcode==SOME_OTHER_ERROR_CODE) baz();
它将按预期工作(在与 Assembly1.dll 和 Assembly2.dll 链接后)
现在,如果你得到一个新版本的 Assembly1.dll,它有
public const int SOME_ERROR_CODE=0x11;
public readonly int SOME_OTHER_ERROR_CODE=0x21;
如果您重新编译 Assembly3.exe 并将最后一个片段链接到新的 Assembly1.dll 和未更改的 Assembly2.dll,它将按预期停止工作:
bar() 将不会被正确调用:Assembly2.dll 记住 LITERAL 0x20,这与 Assembly3.exe 从 Assembly1.dll 中读取的文字 0x21 不同
baz() 将被正确调用:Assembly2.dll 和 Assembly3.exe 都引用名为 SOME_OTHER_ERROR_CODE 的 SYMBOL REFERENCE,在这两种情况下都由 Assembly1.dll 的当前版本解析,因此在这两种情况下都是 0x21。
简而言之: aconst创建 a LITERAL, areadonly创建 a SYMBOL REFERENCE。
LITERALS是框架内部的,不能被编组,因此被本机代码使用。
所以
public static readonly String Empty = "";
创建一个symbol reference(在首次使用时通过调用 String cosntuctor 解决),可以编组一个因此从本机使用的,而
public static const String Empty = "";
会创建一个文字,但不能。