Usage of hash code causes comparison to return wrong result
See original GitHub issueIt seems like the code uses hash codes to prevent cycles during the comparison of objects. The hash code is used as a key for a dictionary. I think this is not a good idea because hash codes do not have to be unique. It is absolutely valid for two different objects to have the same hash code. We actually have exactly a situation where comparison fails for this reason.
Here is some example code which should show the problem (the test fails)
[TestClass]
public class TestHashCode
{
[TestMethod]
public void Test_SameHashCode()
{
Details details1 = new Details { fooType = new FooType { typeId = 1 }, additionalInfo = "info" };
Details details2 = new Details { fooType = new FooType { typeId = 2 }, additionalInfo = "info" };
CompareLogic logic = new CompareLogic();
ComparisonResult cmpResult = logic.Compare(details1, details2);
Assert.IsFalse(cmpResult.AreEqual);
}
}
public class FooType
{
public int typeId;
public override int GetHashCode()
{
return typeId;
}
public override bool Equals(object obj)
{
FooType other = obj as FooType;
if (other == null)
return false;
return this.typeId == other.typeId;
}
}
public class Details
{
public FooType fooType;
public string additionalInfo;
public override int GetHashCode()
{
return this.fooType.GetHashCode();
}
public override bool Equals(object obj)
{
Details other = obj as Details;
if (other == null)
return false;
if (this.fooType.Equals(other.fooType) == false)
return false;
return this.additionalInfo.Equals(other.additionalInfo);
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Why can hashCode() return the same value for different ...
hashing an object means "finding a good, descriptive value (number) that can be reproduced by the very same instance again and again".
Read more >The 3 things you should know about hashCode()
So you will have equal objects with different hash codes. For example, calling contains() on a HashMap will return false, even though the...
Read more >Java equals() and hashCode()
If we only use hashCode() and don't implement equals() then also value will be not retrieved because equals() method will return false.
Read more >How (not) to break your app with hashCode() and equals()
The basic rule of the contract states that if two objects are equal to each other based on equals() method, then the hash...
Read more >Java hashCode() and equals() Methods
Most Java classes override this method to provide their own comparison logic. hashcode() – returns a unique integer value for the object in ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

No thank you.
Don’t you even want to give a reason why you won’t fix it?