iequatable

Set's contains method returns different value at different time

那年仲夏 提交于 2019-12-02 08:27:27
问题 I was thinking about how Swift ensures uniqueness for Set because I have turned one of my obj from Equatable to Hashable for free and so I came up with this simple Playground struct SimpleStruct: Hashable { let string: String let number: Int static func == (lhs: SimpleStruct, rhs: SimpleStruct) -> Bool { let areEqual = lhs.string == rhs.string print(lhs, rhs, areEqual) return areEqual } } var set = Set<SimpleStruct>() let first = SimpleStruct(string: "a", number: 2) set.insert(first) So my

How to implement IEquatable<T> when mutable fields are part of the equality - Problem with GetHashCode

假装没事ソ 提交于 2019-12-02 07:27:05
I am using Entity Framework in my application. I implemented with the partial class of an entity the IEquatable<T> interface: Partial Class Address : Implements IEquatable(Of Address) 'Other part generated Public Overloads Function Equals(ByVal other As Address) As Boolean _ Implements System.IEquatable(Of Address).Equals If ReferenceEquals(Me, other) Then Return True Return AddressId = other.AddressId End Function Public Overrides Function Equals(ByVal obj As Object) As Boolean If obj Is Nothing Then Return MyBase.Equals(obj) If TypeOf obj Is Address Then Return Equals(DirectCast(obj, Address

Set's contains method returns different value at different time

房东的猫 提交于 2019-12-02 04:33:36
I was thinking about how Swift ensures uniqueness for Set because I have turned one of my obj from Equatable to Hashable for free and so I came up with this simple Playground struct SimpleStruct: Hashable { let string: String let number: Int static func == (lhs: SimpleStruct, rhs: SimpleStruct) -> Bool { let areEqual = lhs.string == rhs.string print(lhs, rhs, areEqual) return areEqual } } var set = Set<SimpleStruct>() let first = SimpleStruct(string: "a", number: 2) set.insert(first) So my first question was: Will the static func == method be called anytime I insert a new obj inside the set?

IEquatable Interface what to do when checking for null

倖福魔咒の 提交于 2019-11-30 21:25:08
I have implemented the IEquatable interface in a class with the following code. public bool Equals(ClauseBE other) { if (this._id == other._id) { return true; } return false; } public override bool Equals(Object obj) { if (obj == null) { return base.Equals(obj); } if (!(obj is ClauseBE)) { throw new InvalidCastException("The 'obj' argument is not a ClauseBE object."); } return Equals(obj as ClauseBE); } public override int GetHashCode() { return this._id.GetHashCode(); } public static bool operator ==(ClauseBE a, ClauseBE b) { // cast to object so we call the overloaded Equals function which

Generic class that conforms to Comparable in Swift

与世无争的帅哥 提交于 2019-11-30 13:50:00
I'm attempting to create a simple generic node class that conforms to the Comparable protocol so that I can easily compare nodes without accessing their key. When I attempt to write the < and == functions, however, the compiler doesn't seem to like it. The < and == functions expect a type when defining the Node parameters. This was simple in Java where you defined equality and < internally to the class. Swift asks for it globally. Any thoughts ? Example: func < (lhs:Node<E:Comparable>, rhs:Node<E:Comparable>) -> Bool { return lhs.key < rhs.key } func == (lhs:Node<E:Comparable>, rhs:Node<E

Is it important to override Equals if I'm implementing IEquatable<T>?

只愿长相守 提交于 2019-11-30 08:37:28
问题 I know the importance of overriding GetHashCode when implementing custom equality checks - for which I have implemented IEquality<T> interface, and also the difference between generic and non-generic Equals as discussed here. Now is there a point to override Equals(object t) ? Wouldn't everything come under generic Equals(T t) ? public override int GetHashCode() //required for hashsets and dictionaries { return Id; } public bool Equals(T other) //IEquatable<T> here { return Id == other.Id; }

IEquatable Interface what to do when checking for null

时光总嘲笑我的痴心妄想 提交于 2019-11-30 04:56:36
问题 I have implemented the IEquatable interface in a class with the following code. public bool Equals(ClauseBE other) { if (this._id == other._id) { return true; } return false; } public override bool Equals(Object obj) { if (obj == null) { return base.Equals(obj); } if (!(obj is ClauseBE)) { throw new InvalidCastException("The 'obj' argument is not a ClauseBE object."); } return Equals(obj as ClauseBE); } public override int GetHashCode() { return this._id.GetHashCode(); } public static bool

Generic class that conforms to Comparable in Swift

喜你入骨 提交于 2019-11-29 19:54:10
问题 I'm attempting to create a simple generic node class that conforms to the Comparable protocol so that I can easily compare nodes without accessing their key. When I attempt to write the < and == functions, however, the compiler doesn't seem to like it. The < and == functions expect a type when defining the Node parameters. This was simple in Java where you defined equality and < internally to the class. Swift asks for it globally. Any thoughts ? Example: func < (lhs:Node<E:Comparable>, rhs

Is it important to override Equals if I'm implementing IEquatable<T>?

若如初见. 提交于 2019-11-29 06:58:43
I know the importance of overriding GetHashCode when implementing custom equality checks - for which I have implemented IEquality<T> interface, and also the difference between generic and non-generic Equals as discussed here. Now is there a point to override Equals(object t) ? Wouldn't everything come under generic Equals(T t) ? public override int GetHashCode() //required for hashsets and dictionaries { return Id; } public bool Equals(T other) //IEquatable<T> here { return Id == other.Id; } public override bool Equals(object obj) //required?? { return Equals(obj as T); } Unsealed types should

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

旧巷老猫 提交于 2019-11-29 05:55:53
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 a StringBuilder to concatenate them and return the hash code from that string? That is: var str = new