iequatable

What's the difference between IComparable & IEquatable interfaces?

微笑、不失礼 提交于 2019-11-28 03:44:25
both the interfaces seem to compare objects for equality, so what's the major differences between them? IEquatable tests whether two objects are equal. IComparable imposes a total ordering on the objects being compared. For example, IEquatable would tell you that 5 is not equal to 7. IComparable would tell you that 5 comes before 7. IEquatable<T> for equality. IComparable<T> for ordering. In addition to Greg D's answer: You might implement IComparable without implementing IEquatable for a class where a partial ordering makes sense, and where very definitely you wish the consumer to draw the

Understanding IEquatable

大城市里の小女人 提交于 2019-11-27 18:17:38
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> ? Ray Booysen 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(Object) , your overridden implementation is also called in calls to the static Equals(Object,

Should IEquatable<T>, IComparable<T> be implemented on non-sealed classes?

做~自己de王妃 提交于 2019-11-27 17:59:38
Anyone have any opinions on whether or not IEquatable<T> or IComparable<T> should generally require that T is sealed (if it's a class )? This question occurred to me since I'm writing a set of base classes intended to aid in the implementation of immutable classes. Part of the functionality which the base class is intended to provide is automatic implementation of equality comparisons (using the class's fields together with attributes which can be applied to fields to control equality comparisons). It should be pretty nice when I'm finished - I'm using expression trees to dynamically create a

What is the difference between IEqualityComparer<T> and IEquatable<T>?

十年热恋 提交于 2019-11-27 17:13:43
I want to understand the scenarios where IEqualityComparer<T> and IEquatable<T> should be used. The MSDN documentation for both looks very similar. IEqualityComparer<T> is an interface for an object that performs the comparison on two objects of the type T . IEquatable<T> is for an object of type T so that it can compare itself to another. When deciding whether to use IEquatable<T> or IEqualityComparer<T> , one could ask: Is there a preferred way of testing two instances of T for equality, or are there several equally valid ways? If there is only one way of testing two instances of T for

Find index of object in an array of type [SomeProtocol]

删除回忆录丶 提交于 2019-11-27 16:19:33
I have an array called subscribers that stores objects which conform to the protocol JABPanelChangeSubscriber. The protocol is declared as public protocol JABPanelChangeSubscriber { } and my array is declared as: var subscribers = [JABPanelChangeSubscriber]() Now I need to implement a method to add a subscriber to the list, but it first has to check that that subscriber has not already been added before. public func addSubscriber(subscriber: JABPanelChangeSubscriber) { if subscribers.find(subscriber) == nil { // This ensures that the subscriber has never been added before subscribers.append

Is there a complete IEquatable implementation reference?

冷暖自知 提交于 2019-11-27 10:36:49
Many of my questions here on SO concerns IEquatable implementation. I found it being extremely difficult to implement correctly, because there are many hidden bugs in the naïve implementation, and the articles I found about it are quite incomplete. I want to find or write a definitive reference which must include: How to implement IEquatable correctly How to override Equals correctly How to override GetHashCode correctly How to implement the ToString method correctly How to implement the operator == correctly How to implement the operator != correctly Such a complete reference already exists?

When To Use IEquatable<T> And Why

蹲街弑〆低调 提交于 2019-11-27 09:58:30
问题 What does IEquatable<T> buy you, exactly? The only reason I can see it being useful is when creating a generic type and forcing users to implement and write a good equals method. What am I missing? 回答1: From the MSDN: The IEquatable(T) interface is used by generic collection objects such as Dictionary(TKey, TValue) , List(T) , and LinkedList(T) when testing for equality in such methods as Contains , IndexOf , LastIndexOf , and Remove . The IEquatable<T> implementation will require one less

Find index of object in an array of type [SomeProtocol]

放肆的年华 提交于 2019-11-27 04:07:56
问题 I have an array called subscribers that stores objects which conform to the protocol JABPanelChangeSubscriber. The protocol is declared as public protocol JABPanelChangeSubscriber { } and my array is declared as: var subscribers = [JABPanelChangeSubscriber]() Now I need to implement a method to add a subscriber to the list, but it first has to check that that subscriber has not already been added before. public func addSubscriber(subscriber: JABPanelChangeSubscriber) { if subscribers.find

Can I overload an == operator on an Interface?

二次信任 提交于 2019-11-26 22:00:18
问题 I have an interface like this: public interface IFoo { int A {get;} int B {get;} } and I have multiple classes implementing IFoo. I want to check equality, not based on ReferenceEquality, but two IFoos should be considered equal, if both A and B is the same (in reality I'm checking a collection of Key-Value pairs sent through WCF, that is why I can't have ReferenceEquality). Now if I have: IFoo first = new FooBar1() { A = 1, B = 1}; IFoo second = new FooBar2() { A = 1, B = 1}; if (first ==

Should IEquatable<T>, IComparable<T> be implemented on non-sealed classes?

不想你离开。 提交于 2019-11-26 19:18:25
问题 Anyone have any opinions on whether or not IEquatable<T> or IComparable<T> should generally require that T is sealed (if it's a class )? This question occurred to me since I'm writing a set of base classes intended to aid in the implementation of immutable classes. Part of the functionality which the base class is intended to provide is automatic implementation of equality comparisons (using the class's fields together with attributes which can be applied to fields to control equality