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()
Firstly, as I think you understand, wherever you implement
Equalsyou MUST also implementGetHashCode. The implementation ofGetHashCodemust reflect the behaviour of theEqualsimplementation but it doesn’t usually use it.See http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx – especially the “Notes to Implementers”
So if you take your example of the
Itemimplementation ofEquals, you’re considering both the values ofidandnameto affect equality. So both of these must contribute to theGetHashCodeimplementation.An example of how you could implement
GetHashCodeforItemwould be along the lines of the following (note you may need to make it resilient to a nullablenamefield):See Eric Lippert’s blog post on guidelines for
GetHashCode– http://ericlippert.com/2011/02/28/guidelines-and-rules-for-gethashcode/As for whether you need to re-implement
GetHashCodein subclasses – Yes if you also overrideEquals– as per the first (and main) point – the implementation of the two must be consistent – if two items are considered equal byEqualsthen they must return the same value fromGetHashCode.Side note:
As a performance improvement on your code (avoid multiple casts):