These two functions do exactly the same thing.
Keys.Contains
exists because Keys
is an ICollection<TKey>
, which defines a Contains
method.
The standard Dictionary<TKey, TValue>.KeyCollection
implementation (the class, not the interface) defines it as
bool ICollection<TKey>.Contains(TKey item){
return dictionary.ContainsKey(item);
}
Since it's implemented explicitly, you can't even call it directly.
You're either seeing the interface, which is what I explained above, or the LINQ Contains()
extension method, which will also call the native implementation since it implements ICollection<T>
.
ICollection<TKey>.Contains
, not the LINQ method. – Adelaadelaida