如何使用对象的标识作为Dictionary <K,V>的键

20 .net c#

是否可以将对象用作关键字Dictonary<object, ...>,使得Dictionary只有在相同的情况下才将对象视为相等?

例如,在下面的代码中,我希望第2行返回11而不是12:

Dictionary<object, int> dict = new Dictionary<object, int>();
object a = new Uri("http://www.google.com");
object b = new Uri("http://www.google.com");

dict[a] = 11;
dict[b] = 12;

Console.WriteLine(a == b);  // Line 1. Returns False, because a and b are different objects.
Console.WriteLine(dict[a]); // Line 2. Returns 12
Console.WriteLine(dict[b]); // Line 3. Returns 12
Run Code Online (Sandbox Code Playgroud)

当前的Dictionary实现使用object.Equals()object.GetHashCode()键; 但我正在寻找一种不同类型的字典,它使用对象的标识作为键(而不是对象的值).在.NET中是否有这样的字典或者我是否必须从头开始实现它?

Jon*_*eet 27

您不需要构建自己的字典 - 您需要构建自己的实现,IEqualityComparer<T>其中使用了哈希和相等的标识.我不认为框架中存在这样的东西,但是由于构建起来很容易RuntimeHelpers.GetHashCode.

public sealed class IdentityEqualityComparer<T> : IEqualityComparer<T>
    where T : class
{
    public int GetHashCode(T value)
    {
        return RuntimeHelpers.GetHashCode(value);
    }

    public bool Equals(T left, T right)
    {
        return left == right; // Reference identity comparison
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经限制T为一个引用类型,这样你最终会得到字典中的对象 ; 如果你将它用于值类型,你可能会得到一些奇怪的结果.(我不知道它会如何起作用;我怀疑它不会.)

有了这个,其余的很容易.例如:

Dictionary<string, int> identityDictionary =
    new Dictionary<string, int>(new IdentityEqualityComparer<string>());
Run Code Online (Sandbox Code Playgroud)

  • @JamesShuttler:即使`==`在执行时被*actual*`T`重载,这里的约束也不会强制`T`成为重载的东西,所以这**将是引用相等.(尝试用字符串,例如......) (3认同)

Dan*_*ker 10

当然其他答案完全正确,但我编写了自己的版本以满足我的需求:

/// <summary>
/// An equality comparer that compares objects for reference equality.
/// </summary>
/// <typeparam name="T">The type of objects to compare.</typeparam>
public sealed class ReferenceEqualityComparer<T> : IEqualityComparer<T>
    where T : class
{
    #region Predefined
    private static readonly ReferenceEqualityComparer<T> instance
        = new ReferenceEqualityComparer<T>();
    /// <summary>
    /// Gets the default instance of the
    /// <see cref="ReferenceEqualityComparer{T}"/> class.
    /// </summary>
    /// <value>A <see cref="ReferenceEqualityComparer<T>"/> instance.</value>
    public static ReferenceEqualityComparer<T> Instance
    {
        get { return instance; }
    }
    #endregion

    /// <inheritdoc />
    public bool Equals(T left, T right)
    {
        return Object.ReferenceEquals(left, right);
    }

    /// <inheritdoc />
    public int GetHashCode(T value)
    {
        return RuntimeHelpers.GetHashCode(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

设计理由:

  • 这堂课是sealed.

    如果课程不是为了扩展而设计的,我将通过密封来避免所有费用.
    - Eric Lippert

    我知道许多人(包括我自己)认为课程确实应该默认密封.
    - Jon Skeet

  • 有一个Instance静态只读属性来公开此类的单个实例.
  • 它使用Object.ReferenceEquals()而不是==因为ReferenceEquals更明确.
  • 它使用RuntimeHelpers.GetHashCode()是因为我不想使用可能被覆盖GetHashCode的对象,这可能与行为不匹配ReferenceEquals.这也避免了空检查.
  • 它有文件.


Kyl*_*lan 6

从 5.0 开始,ReferenceEqualityComparer现在随运行时一起提供。


Oli*_*bes 5

使用您自己的相等比较器

public class ObjectIdentityEqualityComparer : IEqualityComparer<object>
{
    public int GetHashCode(object o)
    {
        return o.GetHashCode();
    }

    public bool Equals(object o1, object o2)
    {
        return object.ReferenceEquals(o1, o2);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,GetHashCode可以覆盖,但关键的检查是使用 进行的Equals


这是 .NET Core 时代之前的一个旧答案。正如 @KyleMcClellan 在他的回答中指出的那样,.NET 5.0 中添加了ReferenceEqualityComparer 类(命名空间:)System.Collections.Generic

您可以通过其静态属性获取实例Instance。在VS中按F12可以检查源代码。Equals来电ReferenceEquals(x, y)来电。GetHashCodeRuntimeHelpers.GetHashCode(obj!)