Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A very common bug on my development team is forgetting to, or incorrectly overriding equals and hashcode.

While the pattern is simple, there's a lot of boilerplate code that goes into doing this correctly. Implementing IEquatable<T>, IEquatable, IComparable<T>, and IComparable.

While sometimes this needs to be done in a custom fashion, almost always this is simple a checking of types followed by a checking of an ordered set of fields.

In the absence of compiler support (while I hope is coming), can a code generator/analysis tool help with this problem?

What would be idea is to simply tag properties or fields with an attribute (say [EC_KEY] (for Equality/Comparison Key)). Then all the appropriate routines for checking those fields/properties (in lexical order) would be generated (using those field's type's corresponding equality or comparison operators and, of course, natural ordering for primitives)

Ideally, the analysis tool would report as error objects where no tags (or explicit overloads) were found, but could have tags like [EQ_ALL] (to use all fields/properties) or [EQ_IGNORE] to explicitly ignore the class.

share|improve this question

closed as off-topic by GlenH7, amon, gnat, MichaelT, Bart van Ingen Schenau May 3 '14 at 14:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – GlenH7, amon, gnat, Community, Bart van Ingen Schenau
If this question can be reworded to fit the rules in the help center, please edit the question.

I wouldn't expect any additional compiler support any time soon. The compiler will give you warnings/errors for things like making == without != or making Equals without GetHashCode, but it does not inspect your code for correctness. That is better left to tools like FXCOP. Are you using FXCOP? – Eric Lippert Jun 8 '11 at 18:28
Yes, we're using FXCOP. Mainly I was thinking code generation because it's quite noisy to put this into a large number of our classes, when the pattern is quite well understood. I could write this using aspects and reflection with a higher than we'd like runtime cost, so I'm asking if something is already out there for it. – Neal Tibrewala Jun 8 '11 at 18:57
what, precisely, are you doing that requires every class to implement equality? – Steven A. Lowe Jun 8 '11 at 19:09
It's certainly not every class. Mostly, it's in data transfer objects (things coming out of a database or going to an external client). An example bug would be that a dev implements ordering in IComparable which isn't a strict ordering because it compares only one field, where you'd need to examine another for a strict ordering. The rules about equal objects need equal hashcodes and always when CommpareTo returns 0 equals should be true are currently wholly manual and not easily inspectable by analysis tools, but codegen (or compiler) on these could enforce those guarantees. – Neal Tibrewala Jun 8 '11 at 19:30
Possible duplicate: – Jim G. Jun 9 '11 at 12:23

The best discussion I've seen about this was by the guy who was the lead developer on Castle ActiveRecord. I've not so far been able to find the original post, but you can read about primary key mapping here. He makes the very valid point that determining equality and deriving a hash code are very closely related to deciding on a primary key for your class. He uses attributes to indicate single or composite primary keys and then the base class takes care of the rest; if you didn't want a base class then you could have some helper object and override equals and gethashcode in every class.

My basic point is go look at the source of Castle ActiveRecord and you will probably get a good idea of how to do this in a nice generic way.

share|improve this answer

Use Resharper.

  1. In the editor, position the caret within a class.
  2. On the main menu, choose ReSharper | Edit | Generate Code, or press Alt+Insert.
  3. From the Generate pop-up menu, select Equality members.
  4. On the Generate equality members page of the Generate wizard, select fields to be compared with the Equals() method.

Here's the original documentation.

share|improve this answer
Unfortunately ReSharper doesn't provides code generation for IComparable and IComparable<T>. – JoanComasFdz Mar 26 '13 at 8:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.