locked
Generic.Dictionary ContainsValue doesnt work RRS feed

  • Question

  • Problem with Generic.Dictionary and Generic.SortedDictionary.
    ContainsValue function of both of these classes work with usuall datatypes. But when you make a collection of the objects of a class that you have defined yourself it doesnt work.

    I know that it requires you to implement IComparable which i have done. And this implementation works fine for the other function - Sort- that demands it.

    Please help.

    Monday, November 21, 2005 3:59 PM

Answers

  • The help says:

    Dictionary<T>.ContainsValue uses EqualityComparer<T>.Default to find the value.

    EqualityComparer<T>.Default uses IEquatable (if T implements this interface), and falls back to Object.Equals and Object.GetHashCode.


    IComparable defines an order of the objects, you need to implement IEquatable to define whether two objects are equal.
    Monday, November 21, 2005 5:11 PM

All replies

  • Problem with Generic.Dictionary and Generic.SortedDictionary.
    ContainsValue function of both of these classes work with usuall datatypes. But when you make a collection of the objects of a class that you have defined yourself it doesnt work.

    I know that it requires you to implement IComparable which i have done. And this implementation works fine for the other function - Sort- that demands it.

    Please help.


    Monday, November 21, 2005 3:57 PM
  • The help says:

    Dictionary<T>.ContainsValue uses EqualityComparer<T>.Default to find the value.

    EqualityComparer<T>.Default uses IEquatable (if T implements this interface), and falls back to Object.Equals and Object.GetHashCode.


    IComparable defines an order of the objects, you need to implement IEquatable to define whether two objects are equal.
    Monday, November 21, 2005 5:11 PM
  • But don't you also need to implement IEquatable?
    Tuesday, November 22, 2005 10:05 AM