?? Coalesce for empty string?

后端 未结 10 1190
甜味超标
甜味超标 2020-12-12 18:30

Something I find myself doing more and more is checking a string for empty (as in \"\" or null) and a conditional operator.

A current example:



        
10条回答
  •  执念已碎
    2020-12-12 19:11

    A slightly faster extension method than proposed earlier perhaps:

    public static string Fallback(this string @this, string @default = "")
    {
        return (@this == null || @this.Trim().Length == 0) ? @default : @this;
    }
    

提交回复
热议问题