Cannot use String.Empty as a default value for an optional parameter

前端 未结 8 1628
生来不讨喜
生来不讨喜 2020-12-04 14:47

I am reading Effective C# by Bill Wagner. In Item 14 - Minimize Duplicate Initialization Logic, he shows the following example of using the new opt

8条回答
  •  天涯浪人
    2020-12-04 15:35

    I use string.Empty purely for readability.

    If someone else needs to read/change my code later they know that I meant to check for or set something to an empty string. Using just "" can sometimes cause bugs and confusion because I may have just forgot to put the string that I wanted in there.

    For example:

    if(someString == string.Empty)
    {
    
    }
    

    vs

    if(someString == "")
    {
    
    }
    

    The first if statement just seems so much more deliberate and readable to me. Because this is just a preference though, I really do not see the train-smash in having to use "" instead of string.Empty.

提交回复
热议问题