views:

190

answers:

2

I'm currently investigating the use of FxCop with one of our existing projects and am getting an odd result.

The output displays a small number of breaches of the 'Override methods on comparable types' rule stating "'Log' should override Equals since it implements IComparable."

There are two issues with this:

  1. I thought that it was only necessary to implement CompareTo when implementing IComparable (MSDN itself confirms this)
  2. The class is not implementing IComparable but IComparable<T> and does impliment CompareTo as strongly typed.

So it FxCop (1.36) throwing a wobbly or is it my understanding thats out of whack here..?

Thanks in advance.

+2  A: 

FxCop is a quite paranoidal tool... In this case, I suppose, it is trying to warn you, that you are changing the logic of comparison somehow and you shouldn't forget changing the equality logic if needed. You see, CompareTo method sometimes returns 0, which should be consistent with using Equals.

If this is not really your case, and you are sure you don't need any of the overloading (an example in MSDN shows that you will need to override all other equalty operators as well)... then just supress the warning or disable it.

Yacoder
+1  A: 

I would override Equals,

  • just call base.Equals in your method
  • and add a comment explaining why the above is all that is needed

That way FxCop is happy, and so is the next programmer that looks at your code. (In a very few cases you can't do the above due to proforance problems, but these are rare these days.

Ian Ringrose