我有一个 Obj 的 HashSet,其中 Obj 定义如下:
public class Obj
{
private int _id;
private string _desc;
private int _sum;
public int Id
{
get { return _id; }
set { _id = value; }
}
public string Description
{
get { return _desc; }
set { _desc = value; }
}
public int Sum
{
get { return _sum; }
set { _sum = value; }
}
public Obj(int id, string desc, int sum)
{
_id = id;
_sum = sum;
_desc = desc;
}
public override bool Equals(Obj other)
{
return this._sum == other._sum
&& this._desc == other._desc;
}
public override int GetHashCode()
{
int hash = 13;
hash = (hash * 7) + _sum.GetHashCode();
hash = (hash * 7) + _desc.GetHashCode();
return hash;
}
}
这很好用,但是当HashSet.Add(obj)
返回 false 时,我无法从 HashSet 中检索。在这种情况下,检索已包含在中_id
的最好的方法是什么?Obj
HashSet