I have this simple example:
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;
}
}
}
This is working fine. But if I change the MyKey struct by a MyKey class in this way:
public class MyKey
Then method TryGetValue
doesn't find any key in spite of the key is out there.
I am sure I am missing something obvious but I don't know what.
Any idea ?
Thanks
(please, see accepted solution for better GetHashCode resolution)
I have redefined MyKey class like this, and all is working fine now:
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();
}
}
Thanks to all people answered this.
You need to override Equals()
and GetHashCode()
in the class MyKey
Maybe something like this:
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;
}
}
Equals
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With