-
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
Changes from 1 commit
5329fc4
237f22a
fb22de9
53c834a
5926f3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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) => | ||
object.ReferenceEquals(this, other) || | ||
!object.ReferenceEquals(other, null) && | ||
this.GetType() == other.GetType() && | ||
object.Equals(_ref1, other._ref1) && | ||
_value1.Equals(other._value1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notes: Some very old articles claim the following is faster than using
but most newer articles say that after JITing they are exactly the same thing. Using We must check that the types are equal. Objects of different types are never equal. My recommendation is that we NEVER use The reason is that in some cases Always use Always use |
||
|
||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
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) => | ||
object.ReferenceEquals(this, other) || | ||
base.Equals(other) && | ||
object.Equals(_ref2, other._ref2) && | ||
_value2.Equals(other._value2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notes: The No need to check for null or compare types, that happens in |
||
|
||
public override int GetHashCode() => 0; // implement as appropriate | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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) => | ||
object.ReferenceEquals(this, other) || | ||
!object.ReferenceEquals(other, null) && | ||
object.Equals(_ref, other._ref) && | ||
_value.Equals(other._value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to compare types because the class is non polymorphic. This assumption is only valid is the class is |
||
|
||
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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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) => | ||
object.Equals(_ref, other._ref) && | ||
_value.Equals(other._value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to check for null because this is a struct. No need to compare types because structs can't be polymorphic. |
||
|
||
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); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.