ContainsKey()
to check if a key exists in a Dictionary<T, K>
CS-P1016If 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
.
if (dict.Keys.Contains(key))
{
// ...
}
if (dict.ContainsKey(key))
{
// ...
}