在枚举上使用XOR运算符重载GetHashCode和相等运算符

时间:2010-02-23 18:47:10

标签: c# operators xor gethashcode

我有以下课程,这是一个统计分析包的一部分。

  • MetricKey对象用作字典键。
  • DecisionMetricUnit& Portfolio都是枚举。

我不得不重写等于运算符(==)以使字典键匹配工作。我在http://msdn.microsoft.com/en-us/library/ms173147.aspx使用了指南。指导说我应该重载我已经完成的GetHashCode方法,但我不明白将我的枚举转换为XOR(^)操作的整数的含义。由于我的枚举整数值重叠,我已经完成了有效还是会得到冲突的哈希码?:

public class MetricKey
{
    public MetricKey(Decision decision, MetricUnit metricUnit, Portfolio portfolio)
    {
        Decision = decision;
        Unit = metricUnit;
        Portfolio = portfolio;
    }

    public Decision Decision { get; private set; }
    public MetricUnit Unit { get; private set; }
    public Portfolio Portfolio { get; private set; }

    public static bool operator == (MetricKey a, MetricKey b)
    {
        if (ReferenceEquals(a, b))
            return true;
        if (((object) a == null) || ((object) b == null))
            return false;
        return a.Decision == b.Decision && a.Unit == b.Unit && a.Portfolio == b.Portfolio;
    }

    public static bool operator != (MetricKey a, MetricKey b)
    {
        return !(a == b);
    }

    public override bool Equals(System.Object obj)
    {
        if (obj == null)
            return false;
        var metricKey = obj as MetricKey;
        if ((System.Object) metricKey == null)
            return false;
        return Decision == metricKey.Decision && Unit == metricKey.Unit && Portfolio == metricKey.Portfolio;
    }

    public bool Equals(MetricKey metricKey)
    {
        if ((object) metricKey == null)
            return false;
        return Decision == metricKey.Decision && Unit == metricKey.Unit && Portfolio == metricKey.Portfolio;
    }

    public override int GetHashCode()
    {
        return (int)Decision ^ (int)Unit ^ (int)Portfolio;
    }
}

1 个答案:

答案 0 :(得分:3)

int的强制转换没有任何问题 - 但是,我实际上会避免使用xor - 很容易与枚举的可能值(1,2,3等)产生冲突。请注意,碰撞不会打破任何东西,但它们会使事情变得更加昂贵。我可能会使用(随机选择从C#编译器处理匿名类型中获取灵感):

int num = -1962473570;
num = (-1521134295 * num) + (int)Decision;
num = (-1521134295 * num) + (int)Unit;
return (-1521134295 * num) + (int)Portfolio;