HashSet< T> .CreateSetComparer()无法指定IEqualityComparer< T&gt ;,是否有替代方法? [英] HashSet<T>.CreateSetComparer() cannot specify IEqualityComparer<T>, is there an alternative?

查看:90
本文介绍了HashSet< T> .CreateSetComparer()无法指定IEqualityComparer< T&gt ;,是否有替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内部源中,这样的构造器 public HashSetEqualityComparer(IEqualityComparer<T> comparer),但是它是内部的,所以我不能使用它.

In the internal source there is such a constructor public HashSetEqualityComparer(IEqualityComparer<T> comparer) but it's internal so I can't use it.

默认情况下,HashSet<T>.CreateSetComparer()仅使用将应用EqualityComparer<T>.Default的无参数构造函数.

By default, HashSet<T>.CreateSetComparer() just uses the parameterless constructor which will apply EqualityComparer<T>.Default.

是否有一种方法可以选择IEqualityComparer<T>来获得HashSetEqualityComparer<T>,而无需从源代码中复制代码?

Is there a way to get a HashSetEqualityComparer<T> with a IEqualityComparer<T> of choice, without copying out the code from the source?

推荐答案

我认为最好的解决方案是使用SetEquals.它可以完成您所需的工作,并且完全与HashSetEqualityComparer相同,但是它可以 解释其比较集中定义的所有自定义比较器.

I think best solution is using SetEquals. It does the job you need and exactly in the same way that HashSetEqualityComparer does but it will account for any custom comparers defined in the sets its comparing.

因此,在您要使用HashSet<T>作为字典键的特定情况下,您需要实现一个IEqualityComparer<HashSet<T>>,该IEqualityComparer<HashSet<T>>使用SetEquals并借用" :

So, in your specific scenario where you want to use a HashSet<T> as a key of a dictionary, you need to implement an IEqualityComparer<HashSet<T>> that makes use of SetEquals and "borrows" the reference source of HashSetEqualityComparer.GetHashCode():

public class CustomHashSetEqualityComparer<T>
    : IEqualityComparer<HashSet<T>>
{
    public bool Equals(HashSet<T> x, HashSet<T> y)
    {
        if (ReferenceEquals(x, null))
            return false;

        return x.SetEquals(y);
    }

    public int GetHashCode(HashSet<T> set)
    {
        int hashCode = 0;

        if (set != null)
        {
            foreach (T t in set)
            {
                hashCode = hashCode ^ 
                    (set.Comparer.GetHashCode(t) & 0x7FFFFFFF);
            }
        }

        return hashCode;
    }
}

但是,是的,这有点麻烦,因为无法直接创建使用自定义比较器的SetEqualityComparer,但是不幸的是,恕我直言,这更多是由于现有实现的错误所致,而不是缺少所需的重载; CreateSetComparer()没有理由不能返回实际上使用集合比较器进行比较的IEqualityComparer,如上面的代码所示.

But yes, its a small pain that there is not way to directly create a SetEqualityComparer that leverages custom comparers but this unfortunate behavior is due, IMHO, more to a bug of the existing implementation than a lack of the needed overload; there is no reason why CreateSetComparer() can't return an IEqualityComparer that actually uses the comparers of the sets its comparing as the code above demonstrates.

如果我有声音,CreateSetComparer()根本不是静态方法.这样,很明显,或者至少可以预见,将使用当前集合的比较器创建返回的任何比较器.

If I had a voice in it, CreateSetComparer() wouldn't be static method at all. It would then be obvious, or at least predictable, that whatever comparer was returned would be created with the current set's comparer.

这篇关于HashSet&lt; T&gt; .CreateSetComparer()无法指定IEqualityComparer&lt; T&gt ;,是否有替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆