【发布时间】:2011-04-19 22:20:00
【问题描述】:
一个类从 HashSet 继承来获得一组独特的对象,使用自定义 EqualKeys(T x, T y) 检查而不是 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);
}
}
代码无法编译,因为我必须将 base.Any 替换为 this.Any。
恐怕我不明白这是为什么?
【问题讨论】: