mam HashSet z OBJS z Celu zdefiniowany jako takie:Najlepszy sposób poszukiwania 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;
}
}
Działa to dobrze, ale mam problemy z dostępem z HashSet gdy HashSet.Add(obj)
zwraca false. Jaki byłby najlepszy sposób pobrania _id
z Obj
, który jest już zawarty w HashSet
w takim przypadku?
Podwójne '_'? Błąd czy nie? –
Każdy powód zmiennych prywatnych. Możesz po prostu zrobić 'public int ID {get; set;}' – gunr2171
@newStackExchangeInstance Przepraszam, że był to literówka. – ashishduh