0

If I add IEquatable to a class, I know that I need to add overrides to Equals, GetHashCode and (for good practice) the == and != operators.

When I override these I will check various properties in the class. However, if another (naive) developer later adds another field, they may forget to update these overrides, causing subtle and hard to find issues.

Is there a way to automatically check for added fields? e.g., an add-in or VS feature that will prevent the code from compiling unless the added field has an 'EquatableIgnore' attribute or they've used the field in all overrides.

Note that I have Resharper (so I can generate these easily) and that this question is about keeping the implementation correct if someone adds to a class with IEquatable, not how to correctly implement IEquatable.

pos
  • 1
  • 2
  • Check out [nDepend](https://www.ndepend.com/). With it you can create a tool to interrogate the static code analysis model to find what you are looking for –  Jan 05 '18 at 04:39
  • Thanks @MickyD, I will have to have a look at it (I've never used nDepend before). Not sure it can make it compile-time safe - just a way to query for such an error from time to time. – pos Jan 08 '18 at 03:23
  • Ah, sorry yes, I did not see you wanted _"that will prevent the code from compiling"_. Perhaps look into making an addin for VS? –  Jan 08 '18 at 03:39

0 Answers0