C#

C#

Made by DeepSource

Use ContainsKey() to check if a key exists in a Dictionary<T, K> CS-P1016

Performance
Critical
Autofix

If you wish to check if a key exists in a Dictionary, consider using the ContainsKey() that ideally has a complexity of O(1). Using the .Keys.Contains() deteriorates the performance as it has a complexity of O(n) where n = number of elements in your Dictionary.

Bad Practice

if (dict.Keys.Contains(key))
{
    // ...
}

Recommended

if (dict.ContainsKey(key))
{
    // ...
}

Reference