1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dictionaryに対するFirstOrNull

Posted at

0. はじめに

Freeradicalの中の人、yamarahです。
Dictionaryに対してFirstOrDefaultすると、一致がない場合にKeyVaulePair(default, default)が得られます。ちょっと困る場合があるよね、という話です。

1. どういった場合に困る

FirstOrDefaultで困るのはDictionaryに、KeyもしくはVauleDefault値の要素を含む場合です。
int0や、stringnullを許容している場合など。
これらの場合に、含まれている要素なのか検索ヒットなしかの区別がつきません。

Dictionary<int, string?> dic = new() { { 0, null } };
var pair1 = dic.First();
var pair2 = dic.FirstOrDefault(_ => false);

// 以下はTrueになる
Console.WriteLine(pair1.Key == pair2.Key && pair1.Value == pair2.Value);

もちろん、それぞれの状況に応じて回避方法はあるでしょう。
しかし、毎度回避方法を探るのではなく、ただ単純にFirstOrNullが欲しいのです。

2. 作ってみた

ということで、作りました。

static class CollectionExtensions
{
    public static KeyValuePair<TKey, TValue>? FirstOrNull<TKey, TValue>(this IDictionary<TKey, TValue> source, Func<KeyValuePair<TKey, TValue>, bool> predicate) where TKey : notnull
    {
        foreach(var pair in source)
        {
            if (predicate(pair)) return pair;
        }
        return null;
    }
}

これならば、

Dictionary<int, string?> dic = new() { { 0, null } };
var pair = dic.FirstOrNull(_ => false);
Console.WriteLine(pair is null); // True

となり、満足です。

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?