Hint to replace HashSet<T> with ISet<T>, not ICollection<T>
Given this method:
private static void Add<TValue>(HashSet<TValue> values) {
values.Add(default(TValue));
if(values.Contains(default(TValue))) {
values.Add(default(TValue));
}
}
Resharper offers a hint (suggestion?) to replace HashSet<TValue> with ISet<TValue>. It could have suggested ICollection<TValue> (ISet<T>'s base interface) since Add() only uses collection methods. Not sure this is actually a bug. The ISet<T> signature *is* more clear, and if the method relies on the collection having certain properties (e.g. no duplicates, fast lookup), then the caller *should* only call it with ISet<T> instances.
Anyway, I thought I'd point it out since the behavior is somewhat unexpected.
Please sign in to leave a comment.
Hello Naju
HashSet<T>.Add method actually implements the 'new bool Add(T item);' method from ISet<T>, which hides the corresponding method from ICollection<T>. This means that it is not quite correct to replace HashSet<T> with ICollection<T>. Let me know if this helps. Thank you!
Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
Yes, that's very helpful. Thanks!