Practical example where Tuple can be used in .Net 4.0?

后端 未结 19 724
后悔当初
后悔当初 2020-12-04 07:44

I have seen the Tuple introduced in .Net 4 but I am not able to imagine where it can be used. We can always make a Custom class or Struct.

19条回答
  •  悲哀的现实
    2020-12-04 08:00

    I don't like the abuse of them, since they produce code that doesn't explain itself, but they're awesome to implement on-the-fly compound keys, since they implement IStructuralEquatable and IStructuralComparable (to use both for lookup and ordering purposes).

    And they combine all of their items' hashcodes, internally; for example, here is Tuple's GetHashCode (taken from ILSpy):

        int IStructuralEquatable.GetHashCode(IEqualityComparer comparer)
        {
            return Tuple.CombineHashCodes(comparer.GetHashCode(this.m_Item1), comparer.GetHashCode(this.m_Item2), comparer.GetHashCode(this.m_Item3));
        }
    

提交回复
热议问题