コードインスペクション: 辞書項目の削除は単一の「Remove」で簡素化できます。
C# の Dictionary<T, T> の Remove メソッドは、キーが見つからない場合でも例外をスローせず、単に false を返します。 これは、キーを削除する前に、 TryGetValue を使用してキーが辞書に存在するかどうかを確認する必要がないことを意味します。
int RemoveValue(Dictionary<int, int> d, int toRemove)
{
if (d.TryGetValue(toRemove, out var result))
{
d.Remove(toRemove);
return result;
}
return -1;
}
int RemoveValue(Dictionary<int, int> d, int toRemove)
{
if (d.Remove(toRemove, out var result))
{
return result;
}
return -1;
}
2026 年 6 月 12 日