I read that HashSet in .net4 will ignore all duplicates. So what I do:
HashSet<medbaseid> medbaseidlist = new HashSet<medbaseid>(); for (int i = 2; i <= rowCount; i++) { medbaseid medbaseid = new medbaseid() { mainClass = xlRange.Cells[i, 1].Value2.ToString(), genName = xlRange.Cells[i, 2].Value2.ToString(), speciality = xlRange.Cells[i, 3].Value2.ToString(), med_type_id = getId(xlRange.Cells[i, 4].Value2.ToString(), id = i-1 ) }; medbaseidlist.Add(medbaseid); }
medbaseid can have the same values ββas the previous object.
But if I check the hashset later at the end, there are duplicate elements. 
equals and gethashcode method, which I added but didnβt help. I also added id to the class. Thus, 2 objects can have the same content, but different id:
public override bool Equals(object obj) { medbaseid medb = (medbaseid)obj; return ((medb.id == this.id) && (medb.genName == this.genName) && (medb.mainClass == this.mainClass) && (medb.med_type_id == this.med_type_id) && (medb.speciality == this.speciality)) ? true : false; } public override int GetHashCode() { return id; }
So now my question is: what am I doing wrong, or is this the wrong way to use a HashSet ? Thanks in advance for any help.
source share