1

I have been asked to override GetHashCode and Equals on a particular class because I am using instances of this class as the key (lookup) in a dictionary.

I've noticed that the class itself does not contain any public properties and the private fields are reference types. Mostly public methods and events.

Do I still need to override these methods? I haven't seen a problem so far during testing. How is equality tested in this scenario? If I do nothing, is the default OK?

Thanks in advance.

Bobbo
  • 1,593
  • 4
  • 13
  • 13
  • 1
    Intuitively, this doesn't make much sense to me. Why do you use the class as a key if it has no unique identifying characteristics? – Cody Gray - on strike May 17 '14 at 11:15
  • What is the reason you have such type of class as the key of a dictionary? This just does not make sense to me. – SoftwareFactor May 17 '14 at 11:15
  • A class with no state should not be used as a dictionary key, because reference equality is all you have in this case. In other words, it is not possible to construct a different object that would match any other object of that class. Another note: classes with no fields should be static. – Sergey Kalinichenko May 17 '14 at 11:15
  • Other commenters: This class might be an "enum class" with a private constructor. And contrary to what you are saying, this makes perfect sense, and it is a good key for dictionary. On the other hand, what I am worried about is the phrase "This class basically wraps a couple of objects". Are those not stored in fields then? Or is OP thinking that auto properties don't have backing fields? – Kris Vandermotten May 17 '14 at 11:19
  • 1
    `the class itself does not contain any fields or properties` and `This class basically wraps a couple of objects, which also don't contain any fields` Both statements are contradictory. – Sriram Sakthivel May 17 '14 at 11:19
  • @dasblinkenlight: "classes with no fields should be static" - this suggestion doesn't suit for all cases. There's no easy way to provide dependency injection for static classes, but this is often needed. Also, stateless services in WCF are implemented in non-static classes without state... and there could be another reasons – Dennis May 17 '14 at 11:23
  • @Dennis What are you going to do with dependency injection without any fields? – Sriram Sakthivel May 17 '14 at 11:24
  • 1
    @SriramSakthivel: suppose I have a service class, which contains business logic, and no state. If this class isn't static, I can easily inject it (or it's dummy implementation for testing purposes). It's impossible with static class. – Dennis May 17 '14 at 11:27
  • @Dennis Well, you have a good point. if not intended to DI then it is often a sign that class without fields may be turned static. – Sriram Sakthivel May 17 '14 at 11:32
  • Why all this talk about making the class static? OP already stated "using instances of this class as the key (lookup) in a dictionary". So there are already multiple instances, and the instance is the dictionary key. This is a common pattern (e.g. to attach external data to a class you don't control). – Joe White May 17 '14 at 11:37
  • My apologies I was not very clear, the class does wrap 2 other objects and these are stored and private readonly fields. – Bobbo May 17 '14 at 11:40

2 Answers2

2

Then you can use the hash of the two private objects as the hash.

hash1 ^ hash2;

And you can compare the values of the read only fields for equality.

The idea is that two identical objects should be equal.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
1

If you want each instance to be a unique dictionary key, you don't need to override Equals and GetHashCode at all. By default, each instance will be a unique hash key.

Community
  • 1
  • 1
Joe White
  • 94,807
  • 60
  • 220
  • 330