?? Coalesce for empty string?

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

    I'm using a string Coalesce extension method of my own. Since those here are using LINQ, and absolutelly wasting resources for time intensive operations (I'm using it in tight loops), I'll share mine:

    public static class StringCoalesceExtension
    {
        public static string Coalesce(this string s1, string s2)
        {
            return string.IsNullOrWhiteSpace(s1) ? s2 : s1;
        }
    }
    

    I think it is quite simple, and you don't even need to bother with null string values. Use it like this:

    string s1 = null;
    string s2 = "";
    string s3 = "loudenvier";
    string s = s1.Coalesce(s2.Coalesce(s3));
    Assert.AreEqual("loudenvier", s);
    

    I use it a lot. One of those "utility" functions you can't live without after first using it :-)

    0 讨论(0)
  • 2020-12-12 19:05

    I know this is an old question - but I was looking for an answer and none of the above fit my need as well as what I ended up using:

    private static string Coalesce(params string[] strings)
    {
        return strings.FirstOrDefault(s => !string.IsNullOrEmpty(s));
    }
    

    Usage:

    string result = Coalesce(s.SiteNumber, s.AltSiteNumber, "No Number");
    

    EDIT: An even more compact way of writing this function would be:

    static string Coalesce(params string[] strings) => strings.FirstOrDefault(s => !string.IsNullOrEmpty(s));
    
    0 讨论(0)
  • 2020-12-12 19:06

    One of the advantages of the null-coalescing operator is that it short-circuits. When the first part isn't null, the second part isn't evaluated. This can be useful when the fallback requires an expensive operation.

    I ended up with:

    public static string Coalesce(this string s, Func<string> func)
    {
        return String.IsNullOrEmpty(s) ? func() : s;
    }
    

    Usage:

    string navigationTitle = model?.NavigationTitle.
        Coalesce(() => RemoteTitleLookup(model?.ID)). // Expensive!
        Coalesce(() => model?.DisplayName);
    
    0 讨论(0)
  • 2020-12-12 19:08

    C# already lets us substitute values for null with ??. So all we need is an extension that converts an empty string to null, and then we use it like this:

    s.SiteNumber.NullIfEmpty() ?? "No Number";
    

    Extension class:

    public static class StringExtensions
    {
        public static string NullIfEmpty(this string s)
        {
            return string.IsNullOrEmpty(s) ? null : s;
        }
        public static string NullIfWhiteSpace(this string s)
        {
            return string.IsNullOrWhiteSpace(s) ? null : s;
        }
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-12 19:19

    how about a string extension method ValueOrDefault()

    public static string ValueOrDefault(this string s, string sDefault)
    {
        if (string.IsNullOrEmpty(s))
            return sDefault;
        return s;
    }
    

    or return null if string is Empty:

    public static string Value(this string s)
    {
        if (string.IsNullOrEmpty(s))
            return null;
        return s;
    }
    

    Didn't try these solutions though.

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