[英]How to use an object's identity as key for Dictionary<K,V>
Is it possible to use an object as a key for a Dictonary<object, ...>
in such a way that the Dictionary treats objects as equal only if they are identical?是否可以将对象用作
Dictonary<object, ...>
的键,以便字典仅在对象相同时才将对象视为相等?
For example, in the code below, I want Line 2 to return 11 instead of 12:例如,在下面的代码中,我希望第 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
The current Dictionary implementation uses object.Equals()
and object.GetHashCode()
on the keys;当前字典实现使用
object.Equals()
和object.GetHashCode()
上的键; but I am looking for a different kind of dictionary that uses the object's identity as a key (instead of the object's value).但我正在寻找一种不同类型的字典,它使用对象的身份作为键(而不是对象的值)。 Is there such a Dictionary in .NET or do I have to implement it from scratch?
.NET 中是否有这样的字典,还是我必须从头开始实现它?
You don't need to build your own dictionary - you need to build your own implementation of IEqualityComparer<T>
which uses identity for both hashing and equality.您不需要构建自己的字典 - 您需要构建自己的
IEqualityComparer<T>
实现,它使用身份进行散列和相等。 I don't think such a thing exists in the framework, but it's easy enough to build due to RuntimeHelpers.GetHashCode
.我认为框架中不存在这样的东西,但由于
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
}
}
I've restricted T
to be a reference type so that you'll end up with objects in the dictionary;我已经将
T
限制为一个引用类型,这样你就会在字典中得到对象; if you used this for value types you could get some odd results.如果您将它用于值类型,您可能会得到一些奇怪的结果。 (I don't know offhand how that would work; I suspect it wouldn't.)
(我不知道这会如何运作;我怀疑它不会。)
With that in place, the rest is easy.有了这个,剩下的就很容易了。 For example:
例如:
Dictionary<string, int> identityDictionary =
new Dictionary<string, int>(new IdentityEqualityComparer<string>());
Of course the other answers are entirely correct, but I wrote my own version to suit my needs:当然,其他答案完全正确,但我编写了自己的版本以满足我的需求:
/// <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);
}
}
Design rationale:设计原理:
sealed
.sealed
。
If the class is not designed to be extended, I'm going to avoid all that expense by sealing it.
如果该类不是为扩展而设计的,我将通过密封它来避免所有这些费用。
— Eric Lippert— 埃里克·利珀特
I know many people (including myself) who believe that classes should indeed be sealed by default.
我知道很多人(包括我自己)相信类确实应该默认密封。
— Jon Skeet—乔恩·斯基特
Instance
static read-only property to expose a single instance of this class.Instance
静态只读属性来公开此类的单个实例。Object.ReferenceEquals()
instead of ==
because ReferenceEquals
is more explicit.Object.ReferenceEquals()
而不是==
因为ReferenceEquals
更明确。RuntimeHelpers.GetHashCode()
because I don't want to use the possibly overridden GetHashCode
of the object, which may not match the behavior of ReferenceEquals
.RuntimeHelpers.GetHashCode()
因为我不想使用对象的可能被覆盖的GetHashCode
,它可能与ReferenceEquals
的行为不匹配。 This also avoids a null-check.Use your own equality comparer使用你自己的相等比较器
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);
}
}
Note that GetHashCode
can be overridden, but the crucial check is made with Equals
.请注意,可以覆盖
GetHashCode
,但关键检查是使用Equals
。
从 5.0 开始, ReferenceEqualityComparer
现在随运行时一起提供。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.