-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5329fc4
CSHARP-4432: POC of standard implementations of Equals.
rstam 237f22a
CSHARP-4432: Don't use == when implementing Equals.
rstam fb22de9
CSHARP-4432: Compare to null first.
rstam 53c834a
CSHARP-4432: Slight variations for when only implementing Equals and …
rstam 5926f3c
CSHARP-4432: Flip order of a couple tests.
rstam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/MongoDB.Bson/EqualsPoc/EqualsOnlyNonPolymorphicClass.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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
34
src/MongoDB.Bson/EqualsPoc/EquatableNonPolymorphicClass.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andother
types are the same? What if someone try to compareEquatableDerivedClass
withEquatableDerivedFromDerivedClass
? As far as I understood the current implementation will returnstrue
is all local variables would be equal.There was a problem hiding this comment.
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 returnfalse
.