-2

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.

Community
  • 1
  • 1
user1617242
  • 53
  • 1
  • 5
  • 3
    It's very hard to tell with just that tiny piece of code. Please provide a [mcve]. – Jon Skeet Nov 15 '16 at 13:11
  • 1
    To put your your fears to rest, **it should work**. Therefore you must have a bug in your code. The little code you've shown could indicate that two lists with the same objects in different order would produce the same hash code but other than that we can tell you nothing about why it does or does not work in a specific case because there isn't enough code to analyze. Please post a [mcve] along with what behavior you expect the code to have (that it doesn't). – Lasse V. Karlsen Nov 15 '16 at 13:15
  • Could you post the Step.GetHashCode() implementation? – Tamas Ionut Nov 15 '16 at 13:23
  • Are you sure that the Lists are referencing the same objects .? – Med.Amine.Touil Nov 15 '16 at 13:24

1 Answers1

0

You need to show the code for GetHashCode() for StepType, Color and Trigger too to know for sure, but if they are not the same references and you are using the default implementation of GetHashCode() for those types then having different references causes different hash-codes to be generated.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431