在c#中,我想用空字符串初始化一个字符串值。

我该怎么做呢? 什么是正确的方法,为什么?

string willi = string.Empty;

or

string willi = String.Empty;

or

string willi = "";

还是别的什么?


当前回答

这完全是一个代码风格的偏好,就像。net如何处理字符串一样。然而,以下是我的观点:

我总是使用BCL类型名称访问静态方法,属性和字段:字符串。Empty或Int32.TryParse(…)或Double。ε

我总是在声明新实例时使用c#关键字:或者string foo = "bar";

我很少使用未声明的字符串字面量,因为我喜欢能够扫描代码,将它们组合成可重用的命名常量。编译器无论如何都会用字面量替换常量,所以这是一种避免神奇的字符串/数字,并通过名称赋予它们更多含义的方法。另外,更改值更容易。

其他回答

字符串。空和字符串。空的是等价的。String是BCL类名;字符串是它的c#别名(或快捷方式,如果你愿意)。与Int32和int相同。更多示例请参见文档。

至于“”,我不太确定。

就我个人而言,我总是使用string.Empty。

几乎每个开发者都知道“”是什么意思。我个人就遇到过String。第一次是空的,必须花一些时间搜索谷歌来确定它们是否真的是完全相同的东西。

I strongly prefer String.Empty, aside from the other reasons to ensure you know what it is and that you have not accidentally removed the contents, but primarily for internationalization. If I see a string in quotes then I always have to wonder whether that is new code and it should be put into a string table. So every time code gets changed/reviewed you need to look for "something in quotes" and yes you can filter out the empty strings but I tell people it is good practice to never put strings in quotes unless you know it won't get localized.

从性能和代码生成的角度来看,确实没有什么区别。在性能测试中,它们在哪个比另一个更快之间来回切换,而且仅以毫秒计。

在查看幕后代码时,您也看不到任何不同。唯一的区别在于IL,即字符串。空使用操作码ldsfld ""使用ldstr操作码,但这只是因为字符串。Empty是静态的,两个指令做相同的事情。 如果你看一下生产出来的组件,它是完全一样的。

c#代码

private void Test1()
{
    string test1 = string.Empty;    
    string test11 = test1;
}

private void Test2()
{
    string test2 = "";    
    string test22 = test2;
}

有密码

.method private hidebysig instance void 
          Test1() cil managed
{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init ([0] string test1,
                [1] string test11)
  IL_0000:  nop
  IL_0001:  ldsfld     string [mscorlib]System.String::Empty
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  stloc.1
  IL_0009:  ret
} // end of method Form1::Test1
.method private hidebysig instance void 
        Test2() cil managed
{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init ([0] string test2,
                [1] string test22)
  IL_0000:  nop
  IL_0001:  ldstr      ""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  stloc.1
  IL_0009:  ret
} // end of method Form1::Test2

汇编代码

        string test1 = string.Empty;
0000003a  mov         eax,dword ptr ds:[022A102Ch] 
0000003f  mov         dword ptr [ebp-40h],eax 

        string test11 = test1;
00000042  mov         eax,dword ptr [ebp-40h] 
00000045  mov         dword ptr [ebp-44h],eax 
        string test2 = "";
0000003a  mov         eax,dword ptr ds:[022A202Ch] 
00000040  mov         dword ptr [ebp-40h],eax 

        string test22 = test2;
00000043  mov         eax,dword ptr [ebp-40h] 
00000046  mov         dword ptr [ebp-44h],eax 

这个话题已经很老很长了,所以如果在其他地方提到过这种行为,请原谅。(并指出我的答案,包括这个)

如果你使用字符串,我发现编译器的行为有所不同。空引号或双引号。如果你不使用用string初始化的string变量,区别就会显现出来。为空或带双引号。

在初始化字符串的情况下。空,然后编译器警告

CS0219 - The variable 'x' is assigned but its value is never used

永远不会触发,而在使用双引号初始化的情况下,您将获得预期的消息。

这种行为在Connect文章中有解释,链接:https://connect.microsoft.com/VisualStudio/feedback/details/799810/c-warning-cs0219-not-reported-when-assign-non-constant-value

基本上,如果我做对了,他们希望允许程序员用函数的返回值设置一个变量用于调试目的,而不用警告消息来打扰他,因此他们只在常量赋值和字符串的情况下限制警告。空不是常数,而是场。

几年后 微软已经关闭了https://connect .microsoft.com网站,因此没有更多可用的讨论。然而,这篇文章https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0219有一个有趣的评论,似乎证实了前面的原因:

只有当变量值为a时,编译器才会生成此警告 编译时常量。指定一个非常量表达式或方法 结果的局部变量,使观察这些更容易 调试器中的表达式。它还使结果可达, 在该变量可达时防止垃圾收集。