Skip to content

Files

Latest commit

 

History

History
26 lines (17 loc) · 676 Bytes

File metadata and controls

26 lines (17 loc) · 676 Bytes

Pattern: Unnecessary call to Dictionary.ContainsKey(key)

Issue: -

Description

There's no need to guard Dictionary.Remove(key) with Dictionary.ContainsKey(key). Dictionary<TKey,TValue>.Remove(TKey) already checks whether the key exists and doesn't throw if it doesn't exist.

Example of incorrect code:

Dictionary<string, int> dictionary = new();
if (dictionary.ContainsKey("name"))
    dictionary.Remove("name");

Example of correct code:

Dictionary<string, int> dictionary = new();
dictionary.Remove("name");

Further Reading