I have some classes as below, i have implemented the Equals(Object)
method for almost all of them. But i don't know how to write GetHashCode()
. As far I used these data types as value type in a Dictionary
Collection, i think i should override GetHashCode()
.
1.I don't know how to implement GetHashCode()
with logic of Equals(Object)
.
2.There are some derived classes, if i override GetHashCode()
and Equals(Object)
for base class ( Param
), is it still necessary to override it for childs?
class Param
{
...
public Int16 id { get; set; }
public String name { get; set; }
...
public override bool Equals(object obj)
{
if ( obj is Param){
Param p = (Param)(obj);
if (id > 0 && p.id > 0)
return (id == p.id);
else if (name != String.Empty && p.name != String.Empty)
return (name.equals(p.name));
else
return object.ReferenceEquals(this, obj);
}
return false;
}
}
class Item
{
public int it_code { get; set; }
public Dictionary<String, Param> paramAr { get; set; }
...
public override bool Equals(Object obj)
{
Item im = new Item();
if (obj is Item)
im = (Item)obj;
else
return false;
if (this.it_code != String.Empty && im.it_code != String.Empty)
if (this.it_code.Equals(im.it_code))
return true;
bool reParams = true;
foreach ( KeyValuePair<String,Param> kvp in paramAr ){
if (kvp.Value != im.paramAr[kvp.Key]) {
reParams = false;
break;
}
}
return reParams;
}
}
class Order
{
public String or_code { get; set; }
public List <Item> items { get; set; }
...
public override bool Equals( Object obj ){
Order o = new Order();
if (obj is Order)
o = (Order)obj;
else
return false;
if (this.or_code != String.Empty && o.or_code != String.Empty)
if (this.or_code.Equals(o.or_code))
return true;
bool flag = true;
foreach( Item i in items){
if (!o.items.Contains(i)) {
flag = false;
break;
}
}
return flag;
}
}
EDIT: i get this warning:
Warning : 'Item' overrides Object.Equals(object o) but does not override Object.GetHashCode()
GetHashCode
, is that when two objects are equal (by callingEquals
) that their hash code must also be equal. Conversely, if both hash codes are equal, that doesn't imply that the objects themselves are equal (though they could be). Anytime you write a custom implementation of eitherEquals
orGetHashCode
, you'll have to write something for the other method to ensure the logic I explained above stays true. – Feuillant