It looks like hash code issues and implementing your own equality logic have been beaten to death, but I cannot seem to find a definitive answer to this.
I have a custom object (Step) that overrides Equals, GetHashCode, ==, and != as suggested by the MSDN documentation. This object by itself works fine; When comparing a Step to a Step, the equality operators work as expected. Note that by equality I'm referring to the values of the Step's properties, not reference equality.
I have a second object (Steps) that stores the Step object in a generic list. Now I want to see if a list of Step objects is equal to another list of Step objects. In the Steps object I overrode the various methods and operators as I had done in Step. For the GetHashCode override, I iterate through the Step list and combine the hash codes:
foreach(var step in steplist.Steps)
{
hash += step.GetHashCode()
}
return hash
Simple. But it does not work. The hash codes for two separate lists with equal Step objects values return different hash codes. I'm assuming I'm not implementing the hash code override correctly.
I'm about ready to hard code the returning hash code to zero and call it a day. Any insight is appreciated.
Edit:
Sorry, I was trying to be brief so as to avoid a wall of text. Here is the hash code generation for the Step object. Taken from What is the best algorithm for an overridden System.Object.GetHashCode?:
public override int GetHashCode()
{
return new { StepType, Color, Trigger, Delay, Index }.GetHashCode();
}
Note that all these properties, aside from Delay and Index, are reference types, not value types if that makes a difference. And to answer someone's question: no the two list I'm comparing are definitely not the same references. They are two separate lists. Perhaps therein lies my issue.