相关疑难解决方法(0)

元组(或数组)作为C#中的字典键

我想在C#中创建一个Dictionary查找表.我需要将一个3元组的值解析为一个字符串.我尝试使用数组作为键,但这不起作用,我不知道还能做什么.在这一点上,我正在考虑制作一本词典字典词典,但这看起来可能不是很漂亮,尽管我会在javascript中这样做.

c# dictionary tuples hashtable

104
推荐指数
5
解决办法
9万
查看次数

ValueType.GetHashCode的原生实现如何工作?

我创建了两个TheKey类型为k1 = {17,1375984}和k2 = {17,1593144}的结构.显而易见,第二个字段中的指针是不同的.但两者都得到相同的哈希码= 346948941.预计会看到不同的哈希码.请参阅下面的代码.

struct TheKey
{
    public int id;
    public string Name;

    public TheKey(int id, string name)
    {
       this.id = id;
       Name = name;
   }
}

static void Main() {
    // assign two different strings to avoid interning
    var k1 = new TheKey(17, "abc");
    var k2 = new TheKey(17, new string(new[] { 'a', 'b', 'c' }));

    Dump(k1); // prints the layout of a structure
    Dump(k2);

    Console.WriteLine("hash1={0}", k1.GetHashCode());
    Console.WriteLine("hash2={0}", k2.GetHashCode());
}

unsafe static void Dump<T>(T s) …
Run Code Online (Sandbox Code Playgroud)

c# hash struct

39
推荐指数
2
解决办法
6769
查看次数

标签 统计

c# ×2

dictionary ×1

hash ×1

hashtable ×1

struct ×1

tuples ×1