东西我发现自己做得越来越正在检查空字符串(如在""或空)和一个条件操作符。

一个当前示例:

s.SiteNumber.IsNullOrEmpty() ? "No Number" : s.SiteNumber;

这仅仅是一个扩展方法,它是等效于:

string.IsNullOrEmpty(s.SiteNumber) ? "No Number" : s.SiteNumber;

由于它是空的,不为空,??不会做的伎俩。 string.IsNullOrEmpty()??版本将是完美的解决方案。我想,必须有这样做(我希望!)的一个清洁的方式,但我一直在亏损找到它。

有谁知道一个更好的方式来做到这一点,即使它只是在.NET 4.0中?

有帮助吗?

解决方案

有没有内置的方式做到这一点。你可以让你的扩展方法返回一个字符串或空,然而,这将允许合并运算工作。这将是奇怪的,但是,我个人更喜欢你目前的做法。

既然你已经在使用一个扩展方法,为什么不只是做一个返回值或默认:

string result = s.SiteNumber.ConvertNullOrEmptyTo("No Number");

其他提示

C#已经让我们用null ??替代值。因此,我们需要的是一种将一个空字符串null延伸,然后我们使用这样的:

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

我知道这是一个老问题 - 但我一直在寻找答案,以上都不是适合我的需要,以及我最终使用:

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

用法:

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

修改 写这个函数的甚至更紧凑的方式将是:

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

我有几个效用扩展,我喜欢使用:

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;
}

编辑:灵感来自 SFSR 的回答,我会从现在开始加入这个变体到我的工具箱:

public static string Coalesce(this string str, params string[] strings)
{
    return (new[] {str})
        .Concat(strings)
        .FirstOrDefault(s => !string.IsNullOrEmpty(s));
}

比早期也许提出了一种稍快扩展方法:

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

一的空合并运算符的优点是,它短路。当第一部分不为空时,第二部分不进行评价。当回退需要昂贵的操作。这可能是有用的。

我以结束:

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

用法:

string navigationTitle = model?.NavigationTitle.
    Coalesce(() => RemoteTitleLookup(model?.ID)). // Expensive!
    Coalesce(() => model?.DisplayName);

我简单地使用一个NullIfEmpty扩展方法,该方法将总是返回null如果字符串是空的,允许?? (空合并运算符)被用作正常。

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

这然后允许??用作正常和使得链接容易阅读。

string string1 = string2.NullIfEmpty() ?? string3.NullIfEmpty() ?? string4;

怎么样的字符串扩展方法ValueOrDefault()

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

或返回null如果字符串是空的:

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

虽然没有尝试以下解决方案。

我用我自己的字符串合并扩展方法。由于这些在这里使用LINQ,并absolutelly时间密集型操作的资源浪费(我用它在紧密循环),我会分享我的:

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

我觉得这是很简单的,你甚至不需要用空字符串值理会。使用这样的:

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

我使用了很多。你们中的一个生活中不可缺少的“效用”功能后,首先用它: - )

我喜欢这个以下扩展方法QQQ的简洁起见,虽然当然操作者什么样的?会更好。但是,我们可以1了这个允许不是两个而是三个字符串选项值进行比较,其中一个遇到的需要,然后现在天天看(见下文第二功能)。

#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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top