Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-339

Check all implementations of Equals, operator == and GetHashCode

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.3
    • Affects Version/s: 1.2
    • Component/s: None
    • None
    • Minor Change

      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.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: