在值类上实现 GetHashCode

发布于 2025-01-06 12:31:03 字数 1083 浏览 0 评论 0原文

我有一个 Money 类,我想知道在这个值类上实现 GetHashCode 的最佳方法是什么,给出 $1!= €1。针对货币 * 值设置加权值是行不通的。

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
    }
}

I have a class Money and I want to know what the best way of implementing GetHashCode on this value class would be give that $1 != €1. Having a weighted value against the currency * value is not going to work.

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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2025-01-13 12:31:03

由于 Currency.Id 似乎是唯一的,只要它是一个非零整数,我会选择

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

Currency.Id 是否为非空stringGuid,以下内容即可解决问题

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

As the Currency.Id seems unique, provided it's a non-zero integer I'd go with

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

Would Currency.Id be a not-empty string or a Guid, the following would do the trick

public override int GetHashCode()
{
    unchecked
    {
        return (Currency.Id.GetHashCode()*397) ^ Value.GetHashCode();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文