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?