Why does Dictionary.ContainsKey throw ArgumentNullException?

Marcin Kaczmarek picture Marcin Kaczmarek · Dec 6, 2013 · Viewed 12.6k times · Source

The documentation states that bool Dictionary<TKey, TValue>.ContainsKey(TKey key) throws an exception in case a null key is passed. Could anyone give a reason for that? Wouldn't it be more practical if it just returned false?

Answer

stuartd picture stuartd · Dec 6, 2013

If ContainsKey(null) returned false it would give the misleading impression that null keys are allowed..