Meaning of confusing comment above “string.Empty” in .NET/BCL source?

前端 未结 1 2013
礼貌的吻别
礼貌的吻别 2020-12-25 12:06

I\'m trying to understand why string.Empty is readonly and not a const. I saw this Post but I don\'t understand the comment Microsoft

相关标签:
1条回答
  • 2020-12-25 12:58

    The important part is not what happens IN this class, but what happens, when another class uses (and links to) it. Let me explain with another example:

    Assume you have a Assembly1.dll containing a class declaring

    public static const int SOME_ERROR_CODE=0x10;
    public static readonly int SOME_OTHER_ERROR_CODE=0x20;
    

    and another class consuming this e.g.

    public int TryFoo() {
        try {foo();}
        catch (InvalidParameterException) {return SOME_ERROR_CODE;}
        catch (Exception) { return SOME_OTHER_ERROR_CODE;}
        return 0x00;
    }
    

    You compile your class into Assembly2.dll and link it against Assembly1.dll, as expected, your method will return 0x10 on invalid parameters, 0x20 on other errors, 0x00 on success.

    Especially, if you create Assembly3.exe containing something like

    int errorcode=TryFoo();
    if (errorcode==SOME_ERROR_CODE) bar();
    else if (errorcode==SOME_OTHER_ERROR_CODE) baz();
    

    It will work as expected (After being linked against Assembly1.dll and Assembly2.dll)

    Now if you get a new version of Assembly1.dll, that has

    public const int SOME_ERROR_CODE=0x11;
    public readonly int SOME_OTHER_ERROR_CODE=0x21;
    

    If you recompile Assembly3.exe and link the last fragment against new Assembly1.dll and unchanged Assembly2.dll, it will stop working as expected:

    bar() will NOT be called correctly: Assembly2.dll remembers the LITERAL 0x20, which is not the same literal 0x21 that Assembly3.exe reads out of Assembly1.dll

    baz() will be called correctly: Both Assembly2.dll and Assembly3.exe refer to the SYMBOL REFERENCE called SOME_OTHER_ERROR_CODE, which is in both cases resolved by the current version of Assembly1.dll, thus in both cases is 0x21.

    In Short: a const creates a LITERAL, a readonly creates a SYMBOL REFERENCE.

    LITERALS are internal to the framework and can not be marshalled and thus used by native code.

    So

    public static readonly String Empty = ""; 
    

    creates a symbol reference (resovled at time of first use by a call to the String cosntuctor), that can be marshalled an thus used from native, while

    public static const String Empty = ""; 
    

    would create a literal, that can't.

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