是否有可能使用对象作为Dictonary<object, ...>的键,以便字典只在对象相同的情况下才将其视为相等?
例如,在下面的代码中,我希望第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当前的字典实现在键上使用object.Equals()和object.GetHashCode();但是我正在寻找一种不同类型的字典,它使用对象的标识作为键(而不是对象的值)。在.NET中有这样的字典吗?还是我必须从头开始实现它?
发布于 2012-01-20 19:28:36
您不需要构建您自己的字典-您需要构建您自己的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
}
}我已经将T限制为引用类型,以便在字典中得到对象;如果将它用于值类型,可能会得到一些奇怪的结果。(我不知道这是怎么回事,我怀疑不会。)
有了这个位置,剩下的就很容易了。例如:
Dictionary<string, int> identityDictionary =
new Dictionary<string, int>(new IdentityEqualityComparer<string>());发布于 2014-01-12 17:45:27
当然,其他答案是完全正确的,但我写了自己的版本,以满足我的需要:
/// <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);
}
}设计原理:
发布于 2012-01-20 19:36:46
使用你自己的平等比较器
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);
}
}请注意,可以重写GetHashCode,但关键的检查是使用Equals进行的。
https://stackoverflow.com/questions/8946790
复制相似问题