184

""文字列の空 (inまたは null など) と条件演算子をチェックすることがますます増えています。

現在の例:

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

これは単なる拡張メソッドであり、次と同等です。

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

空でnullで??はないため、うまくいきません。のstring.IsNullOrEmpty()バージョンは??完璧なソリューションです。これを行うためのよりクリーンな方法が必要であると考えています(願っています!)が、それを見つけるのに途方に暮れています。

.Net 4.0だけであっても、これを行うためのより良い方法を知っている人はいますか?

4

11 に答える 11

168

C# では、すでに値をnullwith に置き換えることができます??。したがって、必要なのは、空の文字列を に変換する拡張機能だけでnullあり、次のように使用します。

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

拡張クラス:

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;
    }
}
于 2010-03-10T20:23:32.123 に答える
80

これを行う組み込みの方法はありません。ただし、拡張メソッドが文字列または null を返すようにすることもできます。これにより、合体演算子が機能するようになります。ただし、これは奇妙です。個人的には、現在のアプローチを好みます。

すでに拡張メソッドを使用しているため、値またはデフォルトを返すメソッドを作成してみませんか。

string result = s.SiteNumber.ConvertNullOrEmptyTo("No Number");
于 2010-03-10T20:03:34.357 に答える
52

これは古い質問であることは知っていますが、答えを探していましたが、上記のどれも私のニーズと最終的に使用したものに適合しませんでした:

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));
于 2011-07-09T22:27:34.733 に答える
16

使用したいユーティリティ拡張機能がいくつかあります。

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));
}
于 2012-04-12T15:54:26.213 に答える
6

おそらく以前に提案されたよりもわずかに速い拡張方法:

public static string Fallback(this string @this, string @default = "")
{
    return (@this == null || @this.Trim().Length == 0) ? @default : @this;
}
于 2012-10-11T08:47:25.723 に答える
4

文字列拡張メソッド 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;
}

ただし、これらのソリューションは試しませんでした。

于 2010-03-10T20:23:54.923 に答える
3

私は独自の文字列 Coalesce 拡張メソッドを使用しています。ここにいるのは LINQ を使用していて、時間のかかる操作のためにリソースを完全に浪費しているため (タイトなループで使用しています)、私のものを共有します。

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

非常に単純で、null 文字列値を気にする必要さえないと思います。次のように使用します。

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

私はそれをたくさん使います。最初に使用した後は、なくてはならない「ユーティリティ」機能の 1 つ :-)

于 2013-01-17T03:33:36.603 に答える