我想为 Nullable 结构编写一个相等比较器。比方说,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
属性(property)?
最佳答案
这很简单 - 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();
}
}
附带说明一下,可空对象已经实现了相等性,其中包括检查空值,因此如果您在编译时知道可空类型,就可以避免所有这些。
关于c# - 可空结构的 IEqualityComparer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31697448/