JetBrains Rider 2026.1 Help

コードインスペクション:「ContainsKey」呼び出しはディクショナリに項目を追加する前に冗長なものです。

このインスペクションは、 if ステートメントを報告し、 ContainsKey をチェックして、 追加 と同一キーおよび同一値を持つインデックス割り当てのどちらかを選択します。 このパターンでは、単一のインデックス割り当てで両方のケースがすでに処理されるため、存在チェックは冗長です。

using System.Collections.Generic; class C { void AddOrUpdate(Dictionary<int, int> map, int key, int value) { if (!map.ContainsKey(key)) { map.Add(key, value); } else { map[key] = value; } } }
using System.Collections.Generic; class C { void AddOrUpdate(Dictionary<int, int> map, int key, int value) { map[key] = value; } }

このインスペクションは、割り当てられた値が同等であり、一度評価しても安全な場合のみを報告します。

2026 年 6 月 12 日