c# - Distinct 上不调用 IEqualityComparer
问题描述
为什么未过滤以下列表中的重复项:
var distinctValues = new[]
{
new Tuple<int[], int[]>(new[] {1, 2}, new[] {3}),
new Tuple<int[], int[]>(new[] {2, 3}, new[] {5}),
new Tuple<int[], int[]>(new[] {1, 2}, new[] {3})
}.Distinct(new TupleEnumerableComparer<int[]>());
我的完整代码如下:
public class TupleEnumerableComparer<T> : IEqualityComparer<Tuple<T, T>> where T : IEnumerable
{
public bool Equals(Tuple<T, T> left, Tuple<T, T> right)
{
if (object.ReferenceEquals(left, right))
{
return true;
}
if (left is null || right is null)
{
return false;
}
return left.Item1.Cast<object>().SequenceEqual(right.Item1.Cast<object>()) &&
left.Item2.Cast<object>().SequenceEqual(right.Item2.Cast<object>())
}
public int GetHashCode(Tuple<T, T> obj)
{
var valuesInObject = obj.GetType()
.GetProperties()
.Select(property => property.GetValue(obj));
var hash = new HashCode();
foreach (var value in valuesInObject)
{
hash.Add(value);
}
return hash.ToHashCode();
}
}
我已经在上述类的 GetHashCode 和 Equals 中放置了断点,但没有一个被拾取。我做错了什么?谢谢您的帮助。
解决方案
推荐阅读
- php - WAMP Windows 上的 Composer 安装错误
- android - Android MotionLayout 不显示动画
- javascript - JavaScript/AppsScript - 使用 while 循环创建数组会覆盖数组中的第一个元素
- mongodb - 计算对象数组中是否存在值 [MongoDB]
- html - 可以使用负载运行器连续刷新网页约 2 小时吗?
- java - 带有 JOptionPane 的 If 语句
- go - 在未连接到 Internet 的 linux 机器上编译 Golang
- vue.js - Vue 将子组件包含到父插槽中
- java - 供 Java 处理的 Flutter 参数
- html - 使用 bootstrap 的 HR 职位