[CSHARP-339] Check all implementations of Equals, operator == and GetHashCode Created: 12/Oct/11  Updated: 02/Apr/15  Resolved: 13/Oct/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.2
Fix Version/s: 1.3

Type: Bug Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-338 BsonDouble.Equals fails on NaN Closed
Backwards Compatibility: Minor Change

 Description   

Some of the implementations of Equals, operator == and GetHashCode are wrong.

For example:

var x = new BsonInt32(1);
var y = new BsonDouble(1.0);
Console.WriteLine(x.Equals);
Console.WriteLine(x.GetHashCode());
Console.WriteLine(y.GetHashCode());

Outputs:

True
23866
1072716558

This violates the contract of Equals and GetHashCode, which states that when two objects are Equal, they should have the same hash code.

Another example:

var r = new BsonDouble(double.NaN);
var s = new BsonDouble(double.NaN);
Console.WriteLine(r == s);
Console.WriteLine(r.Equals(s));

Outputs:

False
False

To be consistent with the double data type == should return false and Equals should return true in this case.

There may be other examples, which is why this ticket says to check all implementations.



 Comments   
Comment by Robert Stam [ 13/Oct/11 ]

Only backward breaking if you were relying on Equals coercing between numeric BsonValues (which == still does) or depending on the Equals comparing double.NaN using == instead of the Equals.

Generated at Wed Feb 07 21:36:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.