是否可以将对象用作关键字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中是否有这样的字典或者我是否必须从头开始实现它?