Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Using Generic List (Of T) .Contains and .Remove - I got it working but I'm still really confused!

$
0
0

I've had an "interesting" couple or hours trying to use Generic List (Of T) .Contains and .Remove.  I'll spare you the details but for a while I thought I'd need two additonal Classes and three additional Functions one of which would be  GetHashCode.  I read and thought I understood why, if I was adding any kind of equality comparer, I'd also need to add the GetHashCode Function. 

But in the end I seem to have needed only to add Implements IEquatable(Of T) to the Class statement and an Equals Function looking like the one below. 

.Remove and .Contains both work and both call the added Equals Function.   What I don't understand is how I am getting away without a GetHashCode Function.  When, in my fumbling around to get the right incantation, I had a Class for some sort of equality comparer, Intellisense was insisting I had to have a GetHashCode Function.  And from my research I understood why a GetHashCode Function would be necessary if you had an equality comparer Function.  But I don't have one and I don't have any compile time or run time errors (yet!). 

Do I need a GetHashCode Function?  And if I don't need a GetHashCode Function why don't I?  Since I have the Equals Function it would make perfect sense to me that I'd need a GetHashCode Function.

Thanks, Bob

Public Overloads Function Equals(ByVal other As ChangeInfo) As Boolean _
                Implements IEquatable(Of ChangeInfo).Equals

        If Me.Index = other.Index AndAlso Me.OldStr = other.OldStr AndAlso Me.NewStr = other.NewStr Then
            Return True
        Else
            Return False

        End If

    End Function



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>