Skip to content

CSHARP-4432: POC of standard implementations of Equals. #1273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/MongoDB.Bson/EqualsPoc/EqualsOnlyBaseClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace MongoDB.Bson.EqualsPoc
{
internal class EqualsOnlyBaseClass
{
public object _ref1;
public int _value1;

public EqualsOnlyBaseClass(object ref1, int value1)
{
_ref1 = ref1;
_value1 = value1;
}

public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
GetType().Equals(obj.GetType()) &&
obj is EqualsOnlyBaseClass other &&
object.Equals(_ref1, other._ref1) &&
_value1.Equals(other._value1);
}

public override int GetHashCode() => 0; // implement as appropriate

// optionally implement == and !=
public static bool operator ==(EqualsOnlyBaseClass lhs, EqualsOnlyBaseClass rhs) => object.Equals(lhs, rhs);
public static bool operator !=(EqualsOnlyBaseClass lhs, EqualsOnlyBaseClass rhs) => !(lhs == rhs);
}
}
28 changes: 28 additions & 0 deletions src/MongoDB.Bson/EqualsPoc/EqualsOnlyDerivedClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace MongoDB.Bson.EqualsPoc
{
internal class EqualsOnlyDerivedClass : EquatableBaseClass
{
private object _ref2;
private int _value2;

public EqualsOnlyDerivedClass(object ref1, int value1, object ref2, int value2)
: base(ref1, value1)
{
_ref2 = ref2;
_value2 = value2;
}

public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
base.Equals(obj) &&
obj is EqualsOnlyDerivedClass other &&
object.Equals(_ref2, other._ref2) &&
_value2.Equals(other._value2);
}

public override int GetHashCode() => 0; // implement as appropriate
}
}
30 changes: 30 additions & 0 deletions src/MongoDB.Bson/EqualsPoc/EqualsOnlyNonPolymorphicClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace MongoDB.Bson.EqualsPoc
{
internal sealed class EqualsOnlyNonPolymorphicClass
{
private object _ref;
private int _value;

public EqualsOnlyNonPolymorphicClass(object @ref, int value)
{
_ref = @ref;
_value = value;
}

public override bool Equals(object obj)
{
if (object.ReferenceEquals(obj, null)) { return false; }
if (object.ReferenceEquals(this, obj)) { return true; }
return
obj is EqualsOnlyNonPolymorphicClass other &&
object.Equals(_ref, other._ref) &&
_value.Equals(other._value);
}

public override int GetHashCode() => 0; // implement as appropriate

// optionally implement == and !=
public static bool operator ==(EqualsOnlyNonPolymorphicClass lhs, EqualsOnlyNonPolymorphicClass rhs) => object.Equals(lhs, rhs);
public static bool operator !=(EqualsOnlyNonPolymorphicClass lhs, EqualsOnlyNonPolymorphicClass rhs) => !(lhs == rhs);
}
}
35 changes: 35 additions & 0 deletions src/MongoDB.Bson/EqualsPoc/EquatableBaseClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;

namespace MongoDB.Bson.EqualsPoc
{
internal class EquatableBaseClass : IEquatable<EquatableBaseClass>
{
public object _ref1;
public int _value1;

public EquatableBaseClass(object ref1, int value1)
{
_ref1 = ref1;
_value1 = value1;
}

public override bool Equals(object obj) =>
Equals(obj as EquatableBaseClass);

public bool Equals(EquatableBaseClass other)
{
if (object.ReferenceEquals(other, null)) { return false; }
if (object.ReferenceEquals(this, other)) { return true; }
return
GetType().Equals(other.GetType()) &&
object.Equals(_ref1, other._ref1) &&
_value1.Equals(other._value1);
}

public override int GetHashCode() => 0; // implement as appropriate

// optionally implement == and !=
public static bool operator ==(EquatableBaseClass lhs, EquatableBaseClass rhs) => object.Equals(lhs, rhs);
public static bool operator !=(EquatableBaseClass lhs, EquatableBaseClass rhs) => !(lhs == rhs);
}
}
32 changes: 32 additions & 0 deletions src/MongoDB.Bson/EqualsPoc/EquatableDerivedClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace MongoDB.Bson.EqualsPoc
{
internal class EquatableDerivedClass : EquatableBaseClass, IEquatable<EquatableDerivedClass>
{
private object _ref2;
private int _value2;

public EquatableDerivedClass(object ref1, int value1, object ref2, int value2)
: base(ref1, value1)
{
_ref2 = ref2;
_value2 = value2;
}

public override bool Equals(object obj) =>
Equals(obj as EquatableDerivedClass);

public bool Equals(EquatableDerivedClass other)
{
if (object.ReferenceEquals(other, null)) { return false; }
if (object.ReferenceEquals(this, other)) { return true; }
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check if this and other types are the same? What if someone try to compare EquatableDerivedClass with EquatableDerivedFromDerivedClass? As far as I understood the current implementation will returns true is all local variables would be equal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the two objects are not of the same type then base.Equals will return false.

base.Equals(other) &&
object.Equals(_ref2, other._ref2) &&
_value2.Equals(other._value2);
}

public override int GetHashCode() => 0; // implement as appropriate
}
}
34 changes: 34 additions & 0 deletions src/MongoDB.Bson/EqualsPoc/EquatableNonPolymorphicClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace MongoDB.Bson.EqualsPoc
{
internal sealed class EquatableNonPolymorphicClass : IEquatable<EquatableNonPolymorphicClass>
{
private object _ref;
private int _value;

public EquatableNonPolymorphicClass(object @ref, int value)
{
_ref = @ref;
_value = value;
}

public override bool Equals(object obj) =>
Equals(obj as EquatableNonPolymorphicClass);

public bool Equals(EquatableNonPolymorphicClass other)
{
if (object.ReferenceEquals(other, null)) { return false; }
if (object.ReferenceEquals(this, other)) { return true; }
return
object.Equals(_ref, other._ref) &&
_value.Equals(other._value);
}

public override int GetHashCode() => 0; // implement as appropriate

// optionally implement == and !=
public static bool operator ==(EquatableNonPolymorphicClass lhs, EquatableNonPolymorphicClass rhs) => object.Equals(lhs, rhs);
public static bool operator !=(EquatableNonPolymorphicClass lhs, EquatableNonPolymorphicClass rhs) => !(lhs == rhs);
}
}
32 changes: 32 additions & 0 deletions src/MongoDB.Bson/EqualsPoc/EquatableStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace MongoDB.Bson.EqualsPoc
{
internal struct EquatableStruct : IEquatable<EquatableStruct>
{
private object _ref;
private int _value;

public EquatableStruct(object @ref, int value)
{
_ref = @ref;
_value = value;
}

public override bool Equals(object obj) =>
obj is EquatableStruct other && Equals(other);

public bool Equals(EquatableStruct other)
{
return
object.Equals(_ref, other._ref) &&
_value.Equals(other._value);
}

public override int GetHashCode() => 0; // implement as appropriate

// optionally implement == and !=
public static bool operator ==(EquatableStruct lhs, EquatableStruct rhs) => lhs.Equals(rhs);
public static bool operator !=(EquatableStruct lhs, EquatableStruct rhs) => !(lhs == rhs);
}
}