Tôi có một HashSet của objs với obj được định nghĩa như vậy:Cách tốt nhất tìm kiếm HashSet
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;
}
}
này hoạt động tốt nhưng tôi đang gặp rắc rối lấy từ HashSet khi HashSet.Add(obj)
lợi nhuận sai. Điều gì sẽ là cách tốt nhất để truy xuất số _id
của Obj
đã được chứa trong số HashSet
trong trường hợp này?
Đôi '_'s? Sai lầm hay không? –
Bất kỳ lý do nào cho các biến riêng tư. Bạn chỉ có thể làm 'public int ID {get; set;}' – gunr2171
@newStackExchangeInstance Xin lỗi đó là lỗi đánh máy. – ashishduh