How to search the same object with different Equal concepts?

Ignacio Soler Garcia

I have several objects that depending on the use case are considered Equal differently.

I need to use these objects as keys for dictionaries and as far as I know Dictionary<> use the Equals() method which limits me to have only one implementation of it.

Is there any workaround to this? I expected to be able to inject a EqualityComparer, a delegate or something so Dictionary<> can use different ways of searching for items.

Thanks.

Jon Skeet

Any one dictionary can only have a single equality comparer. You can't ask it to find a key with respect to a particular equality comparer, because otherwise its stored hash codes will be useless, and it would have to just do a linear search.

If you have multiple equality comparers you want to search across, I'd keep several separate dictionaries, each with a different comparer.

(If you just wanted to be able to specify the equality comparer and had missed it, that's what the Dictionary(IEqualityComparer<TKey> comparer) constructor is for).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Overriding isEqual: so that different objects can equal the same object (sort of)

From Dev

How do I check if an object is equal to a new object of the same class?

From Dev

How do I check if an object is equal to a new object of the same class?

From Dev

how to toggle the same object for different object?

From Dev

how to toggle the same object for different object?

From Java

How to compare two JSON objects with the same elements in a different order equal?

From Dev

How to ensure the hashcode does not equal another object of same type by chance

From Dev

How to generate same object with different values into an ArrayList?

From Dev

How to determine if an object is the same one or different

From Dev

When are pointers to the same object equal?

From Dev

How to search Kiva Loans for "not equal to"?

From Dev

Same object in different classes?

From Dev

Different types of the same object

From Dev

How are c++ concepts different to Haskell typeclasses?

From Dev

How to select the correct date in the same column when data in different rows are equal

From Dev

How can I declare same object name for different class?

From Dev

How Tomcat Classloader separates different Webapps object scope in same JVM?

From Dev

How can the same socket object serve different clients?

From Dev

Android how to access the same object through different activities

From Dev

How to use same object with its value in different class

From Dev

How to avoid javax.persistence.EntityExistsException: different object with the same identifier?

From Dev

How to store two equal String or any object (with different references) in a HashMap/HashSet?

From Dev

In angularJS, how could I go through the variables in one object and set them equal to another object's variables of the same name?

From Dev

Storing same object in different arrays

From Dev

Reference same object with different tags

From Dev

Same object property with different values

From Dev

Using same object in different packages

From Dev

Replace object in a list with object of the same type, but not necessarily equal

From Dev

Why is an object greater/less than or equal to a different object?

Related Related

  1. 1

    Overriding isEqual: so that different objects can equal the same object (sort of)

  2. 2

    How do I check if an object is equal to a new object of the same class?

  3. 3

    How do I check if an object is equal to a new object of the same class?

  4. 4

    how to toggle the same object for different object?

  5. 5

    how to toggle the same object for different object?

  6. 6

    How to compare two JSON objects with the same elements in a different order equal?

  7. 7

    How to ensure the hashcode does not equal another object of same type by chance

  8. 8

    How to generate same object with different values into an ArrayList?

  9. 9

    How to determine if an object is the same one or different

  10. 10

    When are pointers to the same object equal?

  11. 11

    How to search Kiva Loans for "not equal to"?

  12. 12

    Same object in different classes?

  13. 13

    Different types of the same object

  14. 14

    How are c++ concepts different to Haskell typeclasses?

  15. 15

    How to select the correct date in the same column when data in different rows are equal

  16. 16

    How can I declare same object name for different class?

  17. 17

    How Tomcat Classloader separates different Webapps object scope in same JVM?

  18. 18

    How can the same socket object serve different clients?

  19. 19

    Android how to access the same object through different activities

  20. 20

    How to use same object with its value in different class

  21. 21

    How to avoid javax.persistence.EntityExistsException: different object with the same identifier?

  22. 22

    How to store two equal String or any object (with different references) in a HashMap/HashSet?

  23. 23

    In angularJS, how could I go through the variables in one object and set them equal to another object's variables of the same name?

  24. 24

    Storing same object in different arrays

  25. 25

    Reference same object with different tags

  26. 26

    Same object property with different values

  27. 27

    Using same object in different packages

  28. 28

    Replace object in a list with object of the same type, but not necessarily equal

  29. 29

    Why is an object greater/less than or equal to a different object?

HotTag

Archive