是否存在任何类型的“ ReferenceComparer”?在.NET中?

编程入门 行业动态 更新时间:2024-10-26 12:29:16
本文介绍了是否存在任何类型的“ 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中?

本文发布于:2023-11-27 15:47:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   类型   NET   ReferenceComparer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!