What is the difference between String.Empty and “” (empty string)?

后端 未结 17 1524
礼貌的吻别
礼貌的吻别 2020-11-22 03:25

In .NET, what is the difference between String.Empty and \"\", and are they interchangable, or is there some underlying reference or Localization i

相关标签:
17条回答
  • 2020-11-22 04:12

    In .NET prior to version 2.0, "" creates an object while string.Empty creates no objectref, which makes string.Empty more efficient.

    In version 2.0 and later of .NET, all occurrences of "" refer to the same string literal, which means "" is equivalent to .Empty, but still not as fast as .Length == 0.

    .Length == 0 is the fastest option, but .Empty makes for slightly cleaner code.

    See the .NET specification for more information.

    0 讨论(0)
  • 2020-11-22 04:12

    Another difference is that String.Empty generates larger CIL code. While the code for referencing "" and String.Empty is the same length, the compiler doesn't optimize string concatenation (see Eric Lippert's blog post) for String.Empty arguments. The following equivalent functions

    string foo()
    {
        return "foo" + "";
    }
    string bar()
    {
        return "bar" + string.Empty;
    }
    

    generate this IL

    .method private hidebysig instance string foo() cil managed
    {
        .maxstack 8
        L_0000: ldstr "foo"
        L_0005: ret 
    }
    .method private hidebysig instance string bar() cil managed
    {
        .maxstack 8
        L_0000: ldstr "bar"
        L_0005: ldsfld string [mscorlib]System.String::Empty
        L_000a: call string [mscorlib]System.String::Concat(string, string)
        L_000f: ret 
    }
    
    0 讨论(0)
  • 2020-11-22 04:12

    Everybody here gave some good theoretical clarification. I had a similar doubt. So I tried a basic coding on it. And I found a difference. Here's the difference.

    string str=null;
    Console.WriteLine(str.Length);  // Exception(NullRefernceException) for pointing to null reference. 
    
    
    string str = string.Empty;
    Console.WriteLine(str.Length);  // 0
    

    So it seems "Null" means absolutely void & "String.Empty" means It contains some kind of value, but it is empty.

    0 讨论(0)
  • 2020-11-22 04:17

    Since String.Empty is not a compile-time constant you cannot use it as a default value in function definition.

    public void test(int i=0,string s="")
        {
          // Function Body
        }
    
    0 讨论(0)
  • 2020-11-22 04:20

    I tend to use String.Empty rather than "" for one simple, yet not obvious reason: "" and "" are NOT the same, the first one actually has 16 zero width characters in it. Obviously no competent developer is going to put and zero width characters into their code, but if they do get in there, it can be a maintenance nightmare.

    Notes:

    • I used U+FEFF in this example.

    • Not sure if SO is going to eat those characters, but try it yourself with one of the many zero-width characters

    • I only came upon this thanks to https://codegolf.stackexchange.com/

    0 讨论(0)
  • 2020-11-22 04:21

    All instances of "" are the same, interned string literal (or they should be). So you really won't be throwing a new object on the heap every time you use "" but just creating a reference to the same, interned object. Having said that, I prefer string.Empty. I think it makes code more readable.

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