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

I’ve been thinking about this question for a bit and after a bit of consideration I agree that implementing IEquatable<T> and IComparable<T> should only be done on sealed types. I went back and forth for a bit but then I thought of the following test. Under what circumstances should the following ever return false? IMHO, … Read more

Understanding IEquatable

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, Object) method on your … Read more

Is there a complete IEquatable implementation reference?

Implementing IEquatable<T> for a Value Type Implementing IEquatable<T> for a value type is a little bit different than for a reference type. Let’s assume we have the Implement-Your-Own-Value-Type archetype, a Complex number struct. public struct Complex { public double RealPart { get; set; } public double ImaginaryPart { get; set; } } Our first step … Read more

When To Use IEquatable And Why

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 cast for these classes and as a result will be slightly faster than the standard object.Equals … Read more

What’s the difference between IEquatable and just overriding Object.Equals()?

The main reason is performance. When generics were introduced in .NET 2.0 they were able to add a bunch of neat classes such as List<T>, Dictionary<K,V>, HashSet<T>, etc. These structures make heavy use of GetHashCode and Equals. But for value types this required boxing. IEquatable<T> lets a structure implement a strongly typed Equals method so … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)