从 ValueType.cs
**Action: Our algorithm for returning the hashcode is a little bit complex. We look ** for the first non-static field and get it's hashcode. If the type has no ** non-static fields, we return the hashcode of the type. We can't take the ** hashcode of a static member because if that member is of the same type as ** the original type, we'll end up in an infinite loop.
今天当我使用KeyValuePair作为字典中的键(它存储了xml属性名称(枚举)和它的值(字符串))时,我被它咬了,并期望它根据其所有字段计算它的哈希码,但根据实施情况,它只考虑了关键部分.
示例(来自Linqpad的c/p):
void Main()
{
var kvp1 = …Run Code Online (Sandbox Code Playgroud) 我一直在寻找一种方法来存储和检索超过C#的通用Dictionary类提供的单个键的值.
在网上搜索(以及SO本身)向我展示了几个选项:
基于元组的词典
.NET 4.0可以轻松支持通用的Tuple <,>类.这意味着您可以使用任意元组创建一个Dictionary,即
var myDict = new Dictionary<Tuple<Char, Int>, MyClass>();嵌套字典
我了解到你也可以在Dictionaries中嵌套Dictionaries,这使得访问存储的结果类似于访问N维数组.例如:
Dictionary<int, Dictionary<int, Dictionary<Char, MyClass>>>
Run Code Online (Sandbox Code Playgroud)
然后可以通过以下方式获得: MyClass foo = MyData[8][3]['W'];
定界连锁键词典
但是,虽然两者都适用于复杂数据和自定义类,但我想知道它们是否总是必要的.至少对于原始数据,看起来将键与分隔符连接起来同样有效.
//keys are char + int
Dictionary<string, MyClass> myDict = New Dictionary<string, Myclass>();
String input = myChar + "|" + myInt
MyClass foo = myDict[input]
Run Code Online (Sandbox Code Playgroud)
是否存在使这些方法中的一种优于另一种方案的情景?他们会有类似的表演时间吗?或者重点应该放在哪种方法提供最干净,最容易维护的代码上?
思考?
为了寻找词典的快速复合键,我遇到异常,我无法理解也无法证明.
在有限的测试中
Dictionary<KeyValuePair<UInt32, UInt32>, string>
Run Code Online (Sandbox Code Playgroud)
明显慢于(200:1)
Dictionary<KeyValuePair<UInt16, UInt16>, string>
Run Code Online (Sandbox Code Playgroud)
测试两个循环,从0到1000 Populate,然后包含ContainsKey
Poplulate ContainsKey
UInt32 92085 86578
UInt16 2201 431
Run Code Online (Sandbox Code Playgroud)
问题是
new KeyValuePair<UInt32, UInt32>(i, j).GetHashCode();
Run Code Online (Sandbox Code Playgroud)
产生许多重复.
在循环i和j 1024中,仅创建1024个唯一散列值.
基于来自CasperOne的雪崩评论尝试了i*31和j*97(两个素数),这导致105280在1024X1024上独一无二.仍然有很多重复.CasperOne我知道这与随机不一样.但随机输入并不是我的工作.GetHashCode()应该随机化输出.
为什么重复次数很多?
相同的循环
new KeyValuePair<UInt16, UInt16>(i, j).GetHashCode();
Run Code Online (Sandbox Code Playgroud)
产生1024 X 1024唯一哈希码(完美).
Int32有同样的问题.
这些重复哈希值会终止
Dictionary<KeyValuePair<UInt32, UInt32>, string>
Run Code Online (Sandbox Code Playgroud)
与Int16相比,元组还会生成很多重复项,在Int32中不会降级.
生成原始KVP和原始KPV.GetHashCode的时间类似.
与HashSet相同的异常.
Dictionary<KeyValuePair<UInt32, UInt32>, string> dKVPu32 = new Dictionary<KeyValuePair<UInt32, UInt32>, string>();
Dictionary<KeyValuePair<UInt16, UInt16>, string> dKVPu16 = new Dictionary<KeyValuePair<UInt16, UInt16>, string>();
KeyValuePair<UInt32, UInt32> kvpUint32;
KeyValuePair<UInt16, UInt16> kvpUint16;
int range = 1000;
Int32 hashCode;
HashSet<Int32> kvpUint32Hash = new HashSet<Int32>(); …Run Code Online (Sandbox Code Playgroud)