none
Equality Comparers for Tuples RRS feed

  • General discussion

  • I have found System.Tuples to be useful for dictionaries, except when I want to perform case-insensitive compares. Is there any plan to build an equality comparer into the .NET framework?

    For example:

        Imports System.Collections.Generic

        Public Class TupleEqualityComparer(Of T1, T2, T3, T4, T5)
            Implements IEqualityComparer(Of Tuple(Of T1, T2, T3, T4, T5))

            Private mcmp1 As IEqualityComparer(Of T1)
            Private mcmp2 As IEqualityComparer(Of T2)
            Private mcmp3 As IEqualityComparer(Of T3)
            Private mcmp4 As IEqualityComparer(Of T4)
            Private mcmp5 As IEqualityComparer(Of T5)

            Public Sub New(pcmp1 As IEqualityComparer(Of T1),
                           pcmp2 As IEqualityComparer(Of T2),
                           pcmp3 As IEqualityComparer(Of T3),
                           pcmp4 As IEqualityComparer(Of T4),
                           pcmp5 As IEqualityComparer(Of T5))
                If pcmp1 IsNot Nothing Then
                    mcmp1 = pcmp1
                Else
                    mcmp1 = EqualityComparer(Of T1).Default
                End If

                If pcmp2 IsNot Nothing Then
                    mcmp2 = pcmp2
                Else
                    mcmp2 = EqualityComparer(Of T2).Default
                End If

                If pcmp3 IsNot Nothing Then
                    mcmp3 = pcmp3
                Else
                    mcmp3 = EqualityComparer(Of T3).Default
                End If

                If pcmp4 IsNot Nothing Then
                    mcmp4 = pcmp4
                Else
                    mcmp4 = EqualityComparer(Of T4).Default
                End If

                If pcmp5 IsNot Nothing Then
                    mcmp5 = pcmp5
                Else
                    mcmp5 = EqualityComparer(Of T5).Default
                End If
            End Sub

            Public Shadows Function Equals(x As Tuple(Of T1, T2, T3, T4, T5), y As Tuple(Of T1, T2, T3, T4, T5)) As Boolean Implements IEqualityComparer(Of Tuple(Of T1, T2, T3, T4, T5)).Equals
                Return mcmp1.Equals(x.Item1, y.Item1) AndAlso mcmp2.Equals(x.Item2, y.Item2) AndAlso mcmp3.Equals(x.Item3, y.Item3) AndAlso mcmp4.Equals(x.Item4, y.Item4) AndAlso mcmp5.Equals(x.Item5, y.Item5)
            End Function

            Public Shadows Function GetHashCode(obj As Tuple(Of T1, T2, T3, T4, T5)) As Integer Implements IEqualityComparer(Of Tuple(Of T1, T2, T3, T4, T5)).GetHashCode
                Return mcmp1.GetHashCode(obj.Item1) Xor mcmp2.GetHashCode(obj.Item2) Xor mcmp3.GetHashCode(obj.Item3) Xor mcmp4.GetHashCode(obj.Item4) Xor mcmp5.GetHashCode(obj.Item5)
            End Function
        End Class

    Saturday, December 3, 2016 1:28 AM