[Mono-dev] Key matching issue in generic Dictionary [correct message]
Brian Crowell
mono-devel at fluggo.com
Wed Aug 23 10:41:56 EDT 2006
Brian Crowell wrote:
> IEquatable<T> is not enough. You need to override Object.GetHashCode() and
> Object.Equals(). IEquatable<T> is optional, GetHashCode and Equals are mandatory.
Off the top of my head, this is the proper implementation:
class Name : IEquatable<Name>
{
public Name(string value)
{
...
}
public bool Equals(Name obj)
{
Console.WriteLine("Name.Equals(): this: " + this.value + " other: " + obj.Value);
return this.value.Equals(obj.Value);
}
public string Value
{
get{return this.value;}
}
public override bool Equals( object obj ) {
Name other = obj as Name;
if( other == null )
return false;
return Equals(other); // Calls Equals(Name obj)
}
public override int GetHashCode() {
return value.GetHashCode();
}
// Override operator == and operator != as well;
// not necessary, but recommended
}
More information about the Mono-devel-list
mailing list