In C#, should I use string.Empty or String.Empty or “” to intitialize a string?

前端 未结 30 2341
挽巷
挽巷 2020-11-22 02:06

In C#, I want to initialize a string value with an empty string.

How should I do this? What is the right way, and why?

string willi = string.Empty;
         


        
相关标签:
30条回答
  • 2020-11-22 02:39

    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.

    0 讨论(0)
  • 2020-11-22 02:40

    There really is no difference from a performance and code generated standpoint. In performance testing, they went back and forth between which one was faster vs the other, and only by milliseconds.

    In looking at the behind the scenes code, you really don't see any difference either. The only difference is in the IL, which string.Empty use the opcode ldsfld and "" uses the opcode ldstr, but that is only because string.Empty is static, and both instructions do the same thing. If you look at the assembly that is produced, it is exactly the same.

    C# Code

    private void Test1()
    {
        string test1 = string.Empty;    
        string test11 = test1;
    }
    
    private void Test2()
    {
        string test2 = "";    
        string test22 = test2;
    }
    

    IL Code

    .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
    

    Assembly code

            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 
    
    0 讨论(0)
  • 2020-11-22 02:40

    It is totally a code-style preference, do to how .NET handles strings. However, here are my opinions :)

    I always use the BCL Type names when accessing static methods, properties and fields: String.Empty or Int32.TryParse(...) or Double.Epsilon

    I always use the C# keywords when declaring new instances: int i = 0; or string foo = "bar";

    I rarely use undeclared string literals as I like to be able to scan the code to combine them into reusable named constants. The compiler replaces constants with the literals anyway so this is more of a way to avoid magic strings/numbers and to give a little more meaning to them with a name. Plus changing the values is easier.

    0 讨论(0)
  • 2020-11-22 02:42

    Possibly a controversial comment, but, generally, I find that my life is easier when I act consistently with Microsoft. We can't possibly know the full deeply embedded reasons (sometimes highly rigorous, and sometime kludgy, I imagine) for why they do things.

    They use "" in automatically generated files like the Assembly file, so that is what I do. In fact, when I try to replace any below "" with String.Empty, Visual Studio crashes on me. There is probably a logical explanation for this, but with my limited knowledge, if I just do what they do, most of the time, things work out. (Contra: I am aware they some automatically generated files also use String.Empty, which kind of shatters my point. :) )

    <Assembly: System.Reflection.AssemblyCulture("")>
    <Assembly: System.Reflection.AssemblyDescription("")>
    <Assembly: System.Reflection.AssemblyFileVersion("1.0.0.0")>
    <Assembly: System.Reflection.AssemblyKeyFile("")>
    <Assembly: System.Reflection.AssemblyProduct("")>
    <Assembly: System.Reflection.AssemblyTitle("")>
    
    0 讨论(0)
  • 2020-11-22 02:43

    While difference is very, VERY little, the difference still exist.

    1) "" creates object while String.Empty does not. But this object will be created once and will be referenced from the string pool later if you have another "" in the code.

    2) String and string are the same, but I would recommend to use String.Empty (as well as String.Format, String.Copy etc.) since dot notation indicates class, not operator, and having class starting with capital letter conforms to C# coding standards.

    0 讨论(0)
  • 2020-11-22 02:44

    I would favor string.Empty over String.Empty because you can use it without needing to include a using System; in your file.

    As for the picking "" over string.Empty, it is personal preference and should be decided by your team.

    0 讨论(0)
提交回复
热议问题