iequatable

Should I use a concatenation of my string fields as a hash code?

旧时模样 提交于 2019-12-18 04:35:19
问题 I have an Address class in C# that looks like this: public class Address { public string StreetAddress { get; set; } public string RuralRoute { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } } I'm implementing equality and so I need to override the hash code. At first I was going to use the hashcode formula from EJ but then I thought: These are all string fields, can't I just just use

Understanding IEquatable

拈花ヽ惹草 提交于 2019-12-17 15:34:56
问题 When I implement objects that I want to compare using the IEquatable<T> interface: Why do I have to override Equals(object) method if I already implemented Equals(T) ? Can I use == and != operators once I implement IEquatable<T> ? 回答1: From MS Docs article on IEquatable<T>: If you implement IEquatable<T> , you should also override the base class implementations of Equals(Object) and GetHashCode() so that their behavior is consistent with that of the Equals(T) method. If you do override Equals

in IEquatable<T> implementation is reference check necessary

坚强是说给别人听的谎言 提交于 2019-12-12 14:33:34
问题 I have a class that imlements IEquatable<T> . Is it necessary to do a refrence check in Equals() or is it taken care of in the framework? class Foo : IEquatable<Foo> { int value; Bar system; bool Equals(Foo other) { return this == other || ( value == other.value && system.Equals(other.system) ); } } In the above example is the this==other statement superfluous or necessary? Update 1 I understand I need to correct the code as follows: bool Equals(Foo other) { if( other==null ) { return false;

Equals method implementation helpers (C#)

删除回忆录丶 提交于 2019-12-12 07:54:28
问题 Everytime I write some data class, I usually spend so much time writing the IEquatable implementation. The last class I wrote was something like: public class Polygon { public Point[] Vertices { get; set; } } Implementing IEquatable was exaustive. Surely C#3.0/LINQ helps a lot, but the vertices can be shifted and/or in the reverse order, and that adds a lot of complexity to the Equals method. After many unit tests, and corresponding implementation, I gave up, and changed my application to

Equals Remove wrong assignment inside Equals

我们两清 提交于 2019-12-12 03:27:21
问题 I have following class which i am using to compare some objects it looks like it: Imports System.Collections.Generic Public Class Part Implements IEqualityComparer(Of Part) Public _comparisonType As EqualsComparmission Public Sub New(ComparisonType As EqualsComparmission) Me._comparisonType = ComparisonType End Sub Public Sub New() End Sub Public Property PartName() As String Get Return m_PartName End Get Set(value As String) m_PartName = value End Set End Property Private m_PartName As

IEnumerable.Except() and a custom comparer

老子叫甜甜 提交于 2019-12-11 08:12:34
问题 I'm having troubles with the Except() method. Instead of returning the difference, it returns the original set. I've tried implementing the IEquatable and IEqualityComparer in the Account class. I've also tried creating a separate IEqualityComparer class for Account. When the Except() method is called from main, it doesn't seem to call my custom Equals() method, but when I tried Count(), it did call the custom GetHashCode() method! I'm sure I made a trivial mistake somewhere and I hope a

GetHashCode on null fields?

谁说胖子不能爱 提交于 2019-12-06 17:01:13
问题 How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public Address As String Public Overloads Function Equals(ByVal other As Contact) As Boolean _ Implements System.IEquatable(Of Contact).Equals Return Name = other.Name AndAlso Address = other.Address End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If

GetHashCode on null fields?

一曲冷凌霜 提交于 2019-12-04 22:35:40
How do I deal with null fields in GetHashCode function? Module Module1 Sub Main() Dim c As New Contact Dim hash = c.GetHashCode End Sub Public Class Contact : Implements IEquatable(Of Contact) Public Name As String Public Address As String Public Overloads Function Equals(ByVal other As Contact) As Boolean _ Implements System.IEquatable(Of Contact).Equals Return Name = other.Name AndAlso Address = other.Address End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If ReferenceEquals(Me, obj) Then Return True If TypeOf obj Is Contact Then Return Equals(DirectCast(obj,

What should GetHashCode return when object's identifier is null?

十年热恋 提交于 2019-12-03 14:56:40
问题 Which of the following is correct/better, considering that identity property could be null. public override int GetHashCode() { if (ID == null) { return base.GetHashCode(); } return ID.GetHashCode(); } OR public override int GetHashCode() { if (ID != null) { return ID.GetHashCode(); } return 0; } Update 1: Updated 2nd option. Update 2: Below are the Equals implementations: public bool Equals(IContract other) { if (other == null) return false; if (this.ID.Equals(other.ID)) { return true; }

Equals method implementation helpers (C#)

一笑奈何 提交于 2019-12-03 14:14:13
Everytime I write some data class, I usually spend so much time writing the IEquatable implementation. The last class I wrote was something like: public class Polygon { public Point[] Vertices { get; set; } } Implementing IEquatable was exaustive. Surely C#3.0/LINQ helps a lot, but the vertices can be shifted and/or in the reverse order, and that adds a lot of complexity to the Equals method. After many unit tests, and corresponding implementation, I gave up, and changed my application to accept only triangles, which IEquatable implementation required only 11 unit tests to be fully covered.