I've read that a HashSet in .net4 will ignore all the duplicates. So what I do is:
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 in the end, there are duplicate items.
the equals and gethashcode method i added but didn't help. I also added an id to the class. So 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 my question now is: What am I doing wrong, or is this not the right way to use a HashSet
? Thanks in advance for any help.