2012-02-15 2 views
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 
    } 
} 
+0

답변이 도움이 되었습니까? – nulltoken

답변

0

Currency.Id 고유 보인다 바와 같이, 그것은 0이 아닌 integer 내가

public override int GetHashCode() 
{ 
    unchecked 
    { 
     return (Currency.Id*397)^Value.GetHashCode(); 
    } 
} 

겠습니까 Currency.Id와 함께 갈 것입니다 제공 할 A-비어 있지 string 또는 Guid의 트릭을 할 것입니다 다음

public override int GetHashCode() 
{ 
    unchecked 
    { 
     return (Currency.Id.GetHashCode()*397)^Value.GetHashCode(); 
    } 
} 
관련 문제