我想为可空结构写一个相等的比较器。比方说,DateTime?。所以我想出了这样的代码:
public class NullableEntityComparer<TEntity, TType> : IEqualityComparer<TEntity>
where TType : struct
where TEntity : Nullable<TType>
{
public bool Equals(TEntity x, TEntity y)
{
if(!x.HasValue && ! y.HasValue) return true;
if(x.HasValue && y.HasValue) return x.Value == y.Value;
return false;
}
public int GetHashCode(TEntity obj)
{
if (obj == null) throw new ArgumentNullException("obj");
if (obj.HasValue) return obj.Value.GetHashCode();
else return obj.GetHashCode();
}
}编译器不喜欢这样,并告诉我:
'TType?' is not a valid constraint. A type used as a constraint must be an interface, a non-sealed class or a type parameter. 这是一个明确的信息,但是Nullable<T>是一个类,而TType?只是Nullable<TType>的缩写。还是我漏掉了什么?
为什么这不管用?是否有让IEqualityComparer<T>使用T.HasValue属性的解决方案?
发布于 2015-07-29 10:15:23
这很简单-- Nullable<>是一个struct,因此它算作一个密封类,这在约束中是禁止的(显然--如果您使用一个密封类作为约束,就不需要使用泛型类型参数--您已经拥有完全相同的类型)。
但你根本不需要这么做。只需让TType受struct约束,但不要使用TEntity,只需在需要可空时使用TType?:
public class NullableEntityComparer<TType> : IEqualityComparer<TType?>
where TType : struct
{
public bool Equals(TType? x, TType? y)
{
if(!x.HasValue && ! y.HasValue) return true;
if(x.HasValue && y.HasValue) return x.Value.Equals(y.Value);
return false;
}
public int GetHashCode(TType? obj)
{
return obj.GetHashCode();
}
}顺便提一句,零标记已经有了一个相等的实现,其中包括检查空值,所以如果在编译时知道可空类型,那么就可以避免所有这些。
https://stackoverflow.com/questions/31697448
复制相似问题