I have the following class, which is part of an analytic analysis package.
- The
MetricKey object MetricKey used as a dictionary key. Decision , MetricUnit and Portfolio are all listings.
I had to redefine the equality operator (==) in order to find work with the key for the dictionary. I used the manual http://msdn.microsoft.com/en-us/library/ms173147.aspx . The manual said that I should overload the GetHashCode method that I did, but I donβt understand what values ββmy enumerations enumerate for integers for the XOR (^) operation. Is this valid or am I getting conflicting hash codes due to overlapping integer enum values ββ?:
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; } }
source share