Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using base.Any(..) the warning is: 'HashSet' does not contain a definition for 'Any'

Tags:

c#

hashset

A class inherits from HashSet to get a set of unique objects with custom EqualKeys(T x, T y) check instead of IEqualityComparer.

public class UniqueSet<T> : HashSet<T> where T : IKey
{
    public new void Add(T item)
    {
         // .. check item for null, empty key etc.

          if (base.Any(t => UniqueSet<T>.EqualKeys(t, item)))
          {
             throw new ArgumentException(..);
          }
          if (!base.Add(item)) throw new ArgumentException(..);
     }

    private static bool EqualKeys(T x, T y) 
    {
       return ((IKey)x).Key.Equals(((IKey)y).Key, StringComparison.CurrentCultureIgnoreCase);
    }
}

The code doesn't compile because I have to replace base.Any with this.Any.
I am afraid I don't understand why that is?

like image 945
Gerard Avatar asked Dec 05 '25 15:12

Gerard


1 Answers

"Any" is an extension method, not a method of HashSet<T>. It doesn't exist on the base type, so you cannot invoke it explicitly on the base type. When you invoke it with "this.Any", the overload resolution code cannot find "Any" on the type of "this" so it starts looking for extension methods, and finds one.

Not your question, but I'll mention it anyway: why throw if you add to a set that already has such an element? Why not just make it a no-op to do so? By throwing you make it a requirement for the caller of Add to know whether the element is in the set already or not, which seems like an onerous requirement.

like image 179
Eric Lippert Avatar answered Dec 07 '25 05:12

Eric Lippert