icomparable

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

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

difference between IComparable and IComparer [duplicate]

落爺英雄遲暮 提交于 2019-11-27 06:07:38
This question already has an answer here: When to use IComparable<T> Vs. IComparer<T> 8 answers What is the difference between IComparable and IComparer Interfaces? Is it necessary to use this interface always with Array.Sort() method nawfal As the name suggests, IComparable<T> reads out I'm comparable . IComparable<T> when defined for T lets you compare the current instance with another instance of same type. IComparer<T> reads out I'm a comparer, I compare . IComparer<T> is used to compare any two instances of T , typically outside the scope of the instances of T . As to what they are for

When to use IComparable<T> Vs. IComparer<T>

强颜欢笑 提交于 2019-11-27 02:41:42
I'm trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other? Well they are not quite the same thing as IComparer<T> is implemented on a type that is capable of comparing two different objects while IComparable<T> is implemented on types that are able to compare themselves with other instances of the same type. I tend to use IComparable<T> for times when I need to know how another instance relates to this instance. IComparer<T> is useful for sorting collections as the IComparer<T> stands outside of the

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

How to compare values of generic types?

最后都变了- 提交于 2019-11-26 18:47:50
How do I compare values of generic types? I have reduced it to a minimal sample: public class Foo<T> where T : IComparable { private T _minimumValue = default(T); public bool IsInRange(T value) { return (value >= _minimumValue); // <-- Error here } } The error is: Operator '>=' cannot be applied to operands of type 'T' and 'T'. What on earth!? T is already constrained to IComparable , and even when constraining it to value types ( where T: struct ), we still can't apply any of the operators < , > , <= , >= , == or != . (I know that workarounds involving Equals() exist for == and != , but it

difference between IComparable and IComparer [duplicate]

大憨熊 提交于 2019-11-26 11:51:58
问题 This question already has answers here : When to use IComparable<T> Vs. IComparer<T> (8 answers) Closed last year . What is the difference between IComparable and IComparer Interfaces? Is it necessary to use this interface always with Array.Sort() method 回答1: As the name suggests, IComparable<T> reads out I'm comparable . IComparable<T> when defined for T lets you compare the current instance with another instance of same type. IComparer<T> reads out I'm a comparer, I compare . IComparer<T>

When to use IComparable<T> Vs. IComparer<T>

若如初见. 提交于 2019-11-26 10:07:44
问题 I\'m trying to figure out which of these interfaces I need to implement. They both essentially do the same thing. When would I use one over the other? 回答1: Well they are not quite the same thing as IComparer<T> is implemented on a type that is capable of comparing two different objects while IComparable<T> is implemented on types that are able to compare themselves with other instances of the same type. I tend to use IComparable<T> for times when I need to know how another instance relates to

What problem does IStructuralEquatable and IStructuralComparable solve?

吃可爱长大的小学妹 提交于 2019-11-26 07:28:39
问题 I\'ve noticed these two interfaces, and several associated classes, have been added in .NET 4. They seem a bit superfluous to me; I\'ve read several blogs about them, but I still can\'t figure out what problem they solve that was tricky before .NET 4. What use are IStructuralEquatable and IStructuralComparable ? 回答1: All types in .NET support the Object.Equals() method which, by default, compares two types for reference equality . However, sometimes, it also desirable to be able to compare

How to compare values of generic types?

浪子不回头ぞ 提交于 2019-11-26 06:36:35
问题 How do I compare values of generic types? I have reduced it to a minimal sample: public class Foo<T> where T : IComparable { private T _minimumValue = default(T); public bool IsInRange(T value) { return (value >= _minimumValue); // <-- Error here } } The error is: Operator \'>=\' cannot be applied to operands of type \'T\' and \'T\'. What on earth!? T is already constrained to IComparable , and even when constraining it to value types ( where T: struct ), we still can\'t apply any of the