Dictionary Keys.Contains vs. ContainsKey: are they functionally equivalent?
Asked Answered
J

2

23

I am curious to know if these two are functionally equivalent in all cases.

Is it possible that by changing the dictionary's default comparator that these two would be functionally different?

Also, isn't Keys.Contains almost guaranteed to be slower?

Judaist answered 23/11, 2011 at 0:27 Comment(0)
A
26

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>.

Adelaadelaida answered 23/11, 2011 at 0:30 Comment(10)
@ReedCopsey: No, it won't be slower; I checked the source. Also, if the compile-time type of your dictionary reference is the interface, it will call ICollection<TKey>.Contains, not the LINQ method.Adelaadelaida
I see now that I am indeed calling the extension method.Judaist
The only way that the LINQ method would be avoided was if you were using IDictionary<T,U>, not Dictionary<T,U> for your variable, though... right?Leora
@ReedCopsey: That's exactly what I said. (Or if you explicitly cast Keys to ICollection<TKey>)Adelaadelaida
Thanks - (it wasn't clear to me that you were referring to IDictionary<T,U>, from what you typed...) You already had my upvote, though ;)Leora
If you are using System.Linq then myDictionary.Keys.Contains( key ) will statically bind to Enumerable.Contains which is an O(n) operation. DO NOT DO THIS!! You should always use myDictionary.ContainsKey( key ) or even better, myDictionary.TryGetValue(key, out value)Sticktight
@mhand: Wrong on both counts. Enumerable.Contains is O(1) on KeyCollection, and overload resolution will prefer instance methods to extension methods. referencesource.microsoft.com/#System.Core/System/Linq/…Adelaadelaida
@Adelaadelaida Hmm good point, I must have overlooked the cast in the Enumerable.Contains implementation, which will end up calling ICollection<TKey>.Contains and delegate properly like you stated. However, the compiler will bind to Enumerable.Contains unless it is statically known as ICollection<TKey>, but it seems to be a moot point given the cast to ICollection<TKey> in the Enumerable.Contains implementationSticktight
@mhand: Yes, and IDictionary<K, V>.Keys is an ICollection<K>.Adelaadelaida
Also worth noticing that Contains() can accept an IEqualityComparer<T> for a custom equality comparer.Canaster
V
12

Although they are pretty much equivalent for Dictionary<,>, I find it's much safer to stick with ContainsKey().

The reason is that in the future you may decide to use ConcurrentDictionary<,> (to make your code thread-safe), and in that implementation, ContainsKey is significantly faster (since accessing the Keys property does a whole bunch of locking and creates a new collection).

Vrablik answered 24/6, 2014 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.