?? Coalesce for empty string?

后端 未结 10 1185
甜味超标
甜味超标 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:20

    There isn't a built-in way to do this. You could make your extension method return a string or null, however, which would allow the coalescing operator to work. This would be odd, however, and I personally prefer your current approach.

    Since you're already using an extension method, why not just make one that returns the value or a default:

    string result = s.SiteNumber.ConvertNullOrEmptyTo("No Number");
    
    0 讨论(0)
  • 2020-12-12 19:21

    I simply use a NullIfEmpty extension method which will always return null if the string is empty allowing ?? (Null Coalescing Operator) to be used as normal.

    public static string NullIfEmpty(this string s)
    {
        return string.IsNullOrEmpty(s) ? null : s;
    }
    

    This then allows ?? to be used as normal and makes chaining easy to read.

    string string1 = string2.NullIfEmpty() ?? string3.NullIfEmpty() ?? string4;
    
    0 讨论(0)
  • 2020-12-12 19:21

    I like the brevity of the following extension method QQQ for this, though of course an operator like? would be better. But we can 1 up this by allowing not just two but three string option values to be compared, which one encounters the need to handle every now and then (see second function below).

    #region QQ
    
    [DebuggerStepThrough]
    public static string QQQ(this string str, string value2)
    {
        return (str != null && str.Length > 0)
            ? str
            : value2;
    }
    
    [DebuggerStepThrough]
    public static string QQQ(this string str, string value2, string value3)
    {
        return (str != null && str.Length > 0)
            ? str
            : (value2 != null && value2.Length > 0)
                ? value2
                : value3;
    }
    
    
    // Following is only two QQ, just checks null, but allows more than 1 string unlike ?? can do:
    
    [DebuggerStepThrough]
    public static string QQ(this string str, string value2, string value3)
    {
        return (str != null)
            ? str
            : (value2 != null)
                ? value2
                : value3;
    }
    
    #endregion
    
    0 讨论(0)
  • 2020-12-12 19:24

    I have a couple of utility extensions that I like to use:

    public static string OrDefault(this string str, string @default = default(string))
    {
        return string.IsNullOrEmpty(str) ? @default : str;
    }
    
    public static object OrDefault(this string str, object @default)
    {
        return string.IsNullOrEmpty(str) ? @default : str;
    }
    

    Edit: Inspired by sfsr's answer, I'll be adding this variant to my toolbox from now on:

    public static string Coalesce(this string str, params string[] strings)
    {
        return (new[] {str})
            .Concat(strings)
            .FirstOrDefault(s => !string.IsNullOrEmpty(s));
    }
    
    0 讨论(0)
提交回复
热议问题