Jef*_*tes 138
使用的原因static readonly是使用const非托管代码,如Microsoft在共享源公共语言基础结构2.0版本中所示.要查看的文件是sscli20\clr\src\bcl\system\string.cs.
Empty常量保存空字符串值.我们需要调用String构造函数,以便编译器不会将其标记为文字.
将其标记为文字将意味着它不会显示为我们可以从本机访问的字段.
我从CodeProject上这篇方便的文章中找到了这些信息.
bru*_*nde 23
我认为这里存在很多混乱和不良反应.
首先,const字段是static成员(不是实例成员).
检查10.4节C#语言规范的常量.
尽管常量被认为是静态成员,但常量声明既不需要也不允许使用静态修饰符.
如果public const成员是静态的,则不能认为常量将创建新的Object.
鉴于此,以下代码行在创建新Object方面完全相同.
public static readonly string Empty = "";
public const string Empty = "";
Run Code Online (Sandbox Code Playgroud)
以下是Microsoft的一条说明,解释了2之间的区别:
readonly关键字与const关键字不同.const字段只能在字段声明时初始化.可以在声明或构造函数中初始化只读字段.因此,readonly字段可以具有不同的值,具体取决于所使用的构造函数.此外,虽然const字段是编译时常量,但readonly字段可用于运行时常量,...
所以我发现这里唯一合理的答案是杰夫耶茨的.
String.Empty read only instead of a constant?
Run Code Online (Sandbox Code Playgroud)
如果您将任何字符串设为常量,那么编译器会在您调用它的任何地方替换为实际字符串,并且您用相同的字符串填充您的代码,并且当代码运行时还需要一次又一次地从不同的内存中读取该字符串数据。
如果您将字符串保留在一个地方,因为它是String.Empty,则程序只在一个地方保留相同的字符串并读取它,或引用它 - 保持内存中的数据最少。
此外,如果您使用 String.Empty 作为 const 编译任何 dll,并且出于任何原因更改 String.Empty,则编译后的 dll 将不再相同,因为cost使内部代码实际保留字符串的副本每次通话。
例如,请参阅此代码:
public class OneName
{
const string cConst = "constant string";
static string cStatic = "static string";
readonly string cReadOnly = "read only string";
protected void Fun()
{
string cAddThemAll ;
cAddThemAll = cConst;
cAddThemAll = cStatic ;
cAddThemAll = cReadOnly;
}
}
Run Code Online (Sandbox Code Playgroud)
编译器将作为:
public class OneName
{
// note that the const exist also here !
private const string cConst = "constant string";
private readonly string cReadOnly;
private static string cStatic;
static OneName()
{
cStatic = "static string";
}
public OneName()
{
this.cReadOnly = "read only string";
}
protected void Fun()
{
string cAddThemAll ;
// look here, will replace the const string everywhere is finds it.
cAddThemAll = "constant string";
cAddThemAll = cStatic;
// but the read only will only get it from "one place".
cAddThemAll = this.cReadOnly;
}
}
Run Code Online (Sandbox Code Playgroud)
和大会电话
cAddThemAll = cConst;
0000003e mov eax,dword ptr ds:[09379C0Ch]
00000044 mov dword ptr [ebp-44h],eax
cAddThemAll = cStatic ;
00000047 mov eax,dword ptr ds:[094E8C44h]
0000004c mov dword ptr [ebp-44h],eax
cAddThemAll = cReadOnly;
0000004f mov eax,dword ptr [ebp-3Ch]
00000052 mov eax,dword ptr [eax+0000017Ch]
00000058 mov dword ptr [ebp-44h],eax
Run Code Online (Sandbox Code Playgroud)
编辑:更正错字
| 归档时间: |
|
| 查看次数: |
16019 次 |
| 最近记录: |