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

后端 未结 17 1557
礼貌的吻别
礼貌的吻别 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:04

    Eric Lippert wrote (June 17, 2013):
    "The first algorithm I ever worked on in the C# compiler was the optimizer that handles string concatenations. Unfortunately I did not manage to port these optimizations to the Roslyn codebase before I left; hopefully someone will get to that!"

    Here are some Roslyn x64 results as of January 2019. Despite the consensus remarks of the other answers on this page, it does not appear to me that the current x64 JIT is treating all of these cases identically, when all is said and done.

    Note in particular, however, that only one of these examples actually ends up calling String.Concat, and I'm guessing that that's for obscure correctness reasons (as opposed to an optimization oversight). The other differences seem harder to explain.


    default(String)   +   { default(String),   "",   String.Empty }

    static String s00() => default(String) + default(String);
        mov  rax,[String::Empty]
        mov  rax,qword ptr [rax]
        add  rsp,28h
        ret
    
    static String s01() => default(String) + "";
        mov  rax,[String::Empty]
        mov  rax,qword ptr [rax]
        add  rsp,28h
        ret
    
    static String s02() => default(String) + String.Empty;
        mov  rax,[String::Empty]
        mov  rax,qword ptr [rax]
        mov  rdx,rax
        test rdx,rdx
        jne  _L
        mov  rdx,rax
    _L: mov  rax,rdx
        add  rsp,28h
        ret
    

    ""   +   { default(String),   "",   String.Empty }

    static String s03() => "" + default(String);
        mov  rax,[String::Empty]
        mov  rax,qword ptr [rax]
        add  rsp,28h
        ret
    
    static String s04() => "" + "";
        mov  rax,[String::Empty]
        mov  rax,qword ptr [rax]
        add  rsp,28h
        ret
    
    static String s05() => "" + String.Empty;
        mov  rax,[String::Empty]
        mov  rax,qword ptr [rax]
        mov  rdx,rax
        test rdx,rdx
        jne  _L
        mov  rdx,rax
    _L: mov  rax,rdx
        add  rsp,28h
        ret
    

    String.Empty   +   { default(String),   "",   String.Empty }

    static String s06() => String.Empty + default(String);
        mov  rax,[String::Empty]
        mov  rax,qword ptr [rax]
        mov  rdx,rax
        test rdx,rdx
        jne  _L
        mov  rdx,rax
    _L: mov  rax,rdx
        add  rsp,28h
        ret
    
    static String s07() => String.Empty + "";
        mov  rax,[String::Empty]
        mov  rax,qword ptr [rax]
        mov  rdx,rax
        test rdx,rdx
        jne  _L
        mov  rdx,rax
    _L: mov  rax,rdx
        add  rsp,28h
        ret
    
    static String s08() => String.Empty + String.Empty;
        mov  rcx,[String::Empty]
        mov  rcx,qword ptr [rcx]
        mov  qword ptr [rsp+20h],rcx
        mov  rcx,qword ptr [rsp+20h]
        mov  rdx,qword ptr [rsp+20h]
        call F330CF60                 ; <-- String.Concat
        nop
        add  rsp,28h
        ret
    


    Test details

    Microsoft (R) Visual C# Compiler version 2.10.0.0 (b9fb1610)
    AMD64 Release
    [MethodImpl(MethodImplOptions.NoInlining)]
    'SuppressJitOptimization' = false
    

提交回复
热议问题