0
私はクラスMoneyを持っていますが、この値クラスでGetHashCodeを実装する最良の方法が$ 1!= = 1になることを知りたいと思います。通貨*の値に対して重み付けされた値を持つことは機能しません。値クラスでGetHashCodeを実装する
public class Money : System.IEquatable<Money>
{
public Money(Currency c, decimal val)
{
this.Currency = c;
this.Value = val;
}
public Currency Currency
{
get;
protected set;
}
public decimal Value
{
get;
protected set;
}
public override bool Equals(object obj)
{
Money m = obj as Money;
if (m == null){throw new System.ArgumentNullException("m");}
if(m.Currency.Id == this.Currency.Id)
{
if(m.Value == this.Value)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
// What would be the best way of implementing this as €1 != $1
// Currency object contains 2 members, (int) Id and (string) symbol
}
}
この回答は役に立ちましたか? – nulltoken