Lets say I have a class
Public Class Person
Public Name As String
Public Rank As String
End Class
And then I have a dictionary which uses these classes as the Key. I have two Person objects, each with the same string value in Name and Rank, but I find that they are not treated as the same key. Can the dictionary not be used this way?
The dictionary does not know how to compare your user-made classes by default. It does not simply compare all fields within the class. As noted in the remarks section for Dictionary<TKey,TValue>:
Your person class must implement the
IEquatable<T>interface, consisting of anEquals(Person)method, which will return true if the two instances should be treated as equal. Additionally, as noted in remarks for IEquatable<T>: