I believe the OP means that he has two different objects (presumably
classes) that he considers equal - i.e. they represent the same
Customer, but are different instances.
For the record, I wrote a small sample program (see below) that confirms
to the OP requirements as I understand them, and without anything extra it
"just works". I don't even have to use the same type or even a related
type when calling ContainsKey() as that used for the actual key.
I think some clarification is in order, if there's something OP is trying
to do but can't.
Pete
using System;
using System.Collections.Generic;
using System.Text;
namespace TestCustomHashDictionary
{
class Program
{
class TestClassOne
{
private string _str1;
private string _str2;
public TestClassOne(string str1, string str2)
{
_str1 = str1;
_str2 = str2;
}
public override int GetHashCode()
{
return _str1.GetHashCode() ^ _str2.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj != null)
{
return ToString().Equals(obj.ToString());
}
return false;
}
public override string ToString()
{
return _str1 + _str2;
}
}
class TestClassTwo
{
private string _str;
private int _hash;
public TestClassTwo(string str1, string str2)
{
_hash = str1.GetHashCode() ^ str2.GetHashCode();
_str = str1 + str2;
}
public override int GetHashCode()
{
return _hash;
}
public override bool Equals(object obj)
{
return ToString().Equals(obj.ToString());
}
public override string ToString()
{
return _str;
}
}
static void Main(string[] args)
{
Dictionary<object, object> dict = new Dictionary<object,
object>();
dict.Add(new TestClassOne("one", "two"), null);
Console.WriteLine(
"dict.ContainsKey(new TestClassTwo(\"one\", \"two\"): "
+ dict.ContainsKey(new TestClassTwo("one", "two")));
Console.ReadLine();
}
}
}