Pattern: Unnecessary call to Dictionary.ContainsKey(key)
Issue: -
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");