I implemented an alternative that checks the type and, as far as I am sure, implements the equality contract correctly:
https://mercurynuget.imtqy.com/SuperTuples/
To create a class with the implementation of GetHashcode and Equals (plus a ToString as well as Tuple), the hash is optionally cached.
public class Person : Suple<string, string> { public Person(string firstName, string lastName) : base(firstName, lastName, SupleHash.Cached) { } public string FirstName => Item1; public string LastName => Item2; }
The implementation is simpler than Tuple , so you can make more boxing during Equals , but in practice it performs Tuple Equals even 8 times when the hash is cached ( SupleHash.Cached ) and SupleHash.Cached caching minimizes boxing and other Equals calls, since Equals first compares the cached hash.
Available for nuget:
Install-Package SuperTuples
As an example of how this works, this is the code for triplex:
public abstract class Suple<T1, T2, T3> { private readonly T1 _item1; private readonly T2 _item2; private readonly T3 _item3; private readonly int? _cachedHash; protected Suple(T1 item1, T2 item2, T3 item3) { _item1 = item1; _item2 = item2; _item3 = item3; } protected Suple(T1 item1, T2 item2, T3 item3, SupleHash hashMode) { _item1 = item1; _item2 = item2; _item3 = item3; _cachedHash = CalculateHashCode(); } protected T1 Item1 { get { return _item1; } } protected T2 Item2 { get { return _item2; } } protected T3 Item3 { get { return _item3; } } public override bool Equals(object obj) { if (obj == null) return false;
source share