是否存在任何类型的“ ReferenceComparer”?在.NET中? [英] Is there any kind of "ReferenceComparer" in .NET?

查看:78
本文介绍了是否存在任何类型的“ ReferenceComparer”?在.NET中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BCL中有几个地方可以使用 IEqualityComparer 。像 Enumerable.Contains 字典构造器。如果对默认不满意,可以提供比较器。

There are several places in BCL where one can make use of IEqualityComparer. Like Enumerable.Contains or Dictionary Constructor. I can provide my comparer if I'm not happy with the default one.

有时候我想知道集合中是否包含我所引用的那个对象

问题是: BCL中是否存在仅依赖于ReferenceEquals 方法?

Sometimes I want to know whether the collection contains that very object that I have reference to. Not the one that is considered "equal" in any other meaning.
The question is: whether there exists standard equality comparer in the BCL that relies only on ReferenceEquals method?

我写的是这样的:

class ReferenceComparer<T> : IEqualityComparer<T> where T : class
{
    private static ReferenceComparer<T> m_instance;

    public static ReferenceComparer<T> Instance
    {
        get
        {
            return m_instance ?? (m_instance = new ReferenceComparer<T>());
        }
    }

    public bool Equals(T x, T y)
    {
        return ReferenceEquals(x, y);
    }

    public int GetHashCode(T obj)
    {
        return RuntimeHelpers.GetHashCode(obj);
    }
}

我没有进行全面测试,也没有考虑很多场景,但它似乎使 Enumerable。包含词典很高兴。

I didn't test it thoroughly nor considered lots of scenarios, but it seems to make Enumerable.Contains and Dictionary pretty happy.

推荐答案

据我所知,BCL没有公开任何实现 IEqualityComparer< T> 的公共类型。从.NET 4.0开始具有引用相等。

As far as I know, the BCL doesn't expose any public types that implement IEqualityComparer<T> with reference-equality as of .NET 4.0 .

但是,似乎确实有很多内部类型可以做到这一点,例如:

However, there do appear to be a bunch of internal types that do this, such as:


  • System.Dynamic.Utils.ReferenceEqualityComparer< T>
    (在System.Core中)

  • System.Xaml.Schema.ReferenceEqualityComparer< T>
    (在System.Xaml中)。

  • System.Dynamic.Utils.ReferenceEqualityComparer<T> (in System.Core)
  • System.Xaml.Schema.ReferenceEqualityComparer<T> (in System.Xaml).

我用反射器看了这两种类型的实现,您会很高兴地知道它们似乎都以某种方式实现实际上与您的相同相同,只是他们不使用lazy-initia静态实例化(它们在类型的静态构造函数中创建)。

I took a look at the implementations of these two types with reflector, and you'll be happy to know that both of them appear to be implemented in a way that is virtually identical to yours, except that they don't use lazy-initialization for the static instance (they create it in the static constructor for the type).

我在实现中唯一想到的问题是懒惰-初始化不是线程安全的,但是由于实例是便宜的并且没有保持任何状态,因此不应创建任何错误或重大性能问题。但是,如果您想强制执行单例模式,则必须正确执行。

The only possible 'issue' I can think of with your implementation is that the lazy-initialization is not thread-safe, but since instances are 'cheap' and aren't holding onto any state, that shouldn't create any bugs or major performance problems. If you want to enforce the singleton-pattern though, you'll have to do it properly.

这篇关于是否存在任何类型的“ ReferenceComparer”?在.NET中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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