C#通用字典TryGetValue找不到键

fer*_*ega 11 c# dictionary

我有这个简单的例子:

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<MyKey, string> data = new Dictionary<MyKey, string>();
            data.Add(new MyKey("1", "A"), "value 1A");
            data.Add(new MyKey("2", "A"), "value 2A");
            data.Add(new MyKey("1", "Z"), "value 1Z");
            data.Add(new MyKey("3", "A"), "value 3A");

            string myValue;
            if (data.TryGetValue(new MyKey("1", "A"), out myValue))
                Console.WriteLine("I have found it: {0}", myValue );

        }
    }

    public struct MyKey
    {
        private string row;
        private string col;

        public string Row { get { return row; } set { row = value; } }
        public string Column { get { return col; } set { col = value; } }

        public MyKey(string r, string c)
        {
            row = r;
            col = c;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这工作正常.但是,如果我以这种方式通过MyKey类更改MyKey结构:

public class MyKey
Run Code Online (Sandbox Code Playgroud)

然后方法TryGetValue没有找到任何密钥,尽管密钥在那里.

我确信我错过了一些明显的东西,但我不知道是什么.

任何的想法 ?

谢谢


**解决方案**

(请参阅已接受的解决方案以获得更好的GetHashCode解析)

我已经像这样重新定义了MyKey类,现在一切正常:

public class MyKey
{
    private string row;
    private string col;

    public string Row { get { return row; } set { row = value; } }
    public string Column { get { return col; } set { col = value; } }

    public MyKey(string r, string c)
    {
        row = r;
        col = c;
    }

    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is MyKey)) return false;

        return ((MyKey)obj).Row == this.Row && ((MyKey)obj).Column == this.Column;
    }

    public override int GetHashCode()
    {            
        return (this.Row + this.Column).GetHashCode();
    }    
}
Run Code Online (Sandbox Code Playgroud)

感谢所有人的回答.

Ari*_*ion 7

你需要重写Equals(),并GetHashCode()在类MyKey

也许是这样的:

GetHashCode的()

public override int GetHashCode()
{
   return GetHashCodeInternal(Row.GetHashCode(),Column.GetHashCode());
}
//this function should be move so you can reuse it
private static int GetHashCodeInternal(int key1, int key2)
{
    unchecked
    {
        //Seed
        var num = 0x7e53a269;

        //Key 1
        num = (-1521134295 * num) + key1;
        num += (num << 10);
        num ^= (num >> 6);

        //Key 2
        num = ((-1521134295 * num) + key2);
        num += (num << 10);
        num ^= (num >> 6);

        return num;
    }
}
Run Code Online (Sandbox Code Playgroud)

等于

public override bool Equals(object obj)
{
    if (obj == null)
        return false;
    MyKey p = obj as MyKey;
    if (p == null)
        return false;

    // Return true if the fields match:
    return (Row == p.Row) && (Column == p.Column);
}
Run Code Online (Sandbox Code Playgroud)

  • @FerPt,`5 + 1`在你的实现中与`1 + 5`具有相同的哈希值.(或等于相同数字的列/行的任意组合) (2认同)