I have implemented some version of tuple class , and really want to learn more from it .
Can you please point me on some points that I am missing here with my implementation.
class My_tuple<T1, T2> : EqualityComparer<My_tuple<T1, T2>>
{
#region Virables
public T1 First { get; private set; }
public T2 Second { get; private set; }
#endregion
#region Constractors
public My_tuple(T1 first, T2 second)
{
First = first;
Second = second;
}
#endregion
#region Equals && GetHashCode
public override bool Equals(My_tuple<T1, T2> L, My_tuple<T1, T2> R)
{
return EqualityComparer<T1>.Default.Equals(L.First, R.First) && EqualityComparer<T2>.Default.Equals(L.Second , R.Second);
}
public override bool Equals(object obj)
{
return obj is My_tuple<T1, T2> && Equals(this, (My_tuple<T1, T2>)obj);
}
public override int GetHashCode(My_tuple<T1, T2> M)
{
return M.First.GetHashCode() ^ M.Second.GetHashCode();
}
#endregion
#region operators
public static bool operator ==(My_tuple<T1, T2> left, My_tuple<T1, T2> right)
{
return left.Equals(right);
}
public static bool operator !=(My_tuple<T1, T2> left, My_tuple<T1, T2> right)
{
return !(left == right);
}
public static My_tuple<T1, T2> Create<T1, T2>(T1 first, T2 second)
{
return new My_tuple<T1, T2>(first, second);
}
#endregion
}
Thanks.
A few things:
EqualityComparer<T>– you should implementIEqualityComparer<T>.GetHashCode()is non-ideal; it means that for any tuple with the same type of left and right (e.g.My_tuple<int, int>) where the values are equal, you’ll get the same hash code of 0. i.e. (1, 1) has the same hash code as (2, 2) etc. I prefer an “add and multiply” implementation – in this case you could return (say)17 * hash1+31 * hash2. There will still be collisions, but hopefully not as many.Equals(L, R)assumes thatLandRare non-nullCreatemethod is generic, attempting to redeclare the T1 and T2 type parameters. I’m surprised that even compiles, although it’s something I haven’t tried before. Consider creating a static generic method in a non-generic class (e.g. justMy_tuple) to allow type inference to work, e.g.My_tuple.Create(1, "hello")to create aMy_tuple<int, string>