How to get rid of CA2000 warning when ownership is transferred?

后端 未结 4 1864
情深已故
情深已故 2020-12-09 17:08

The following code generates two CA2000 warnings (among others, but that\'s not the point).

public sealed class Item: IDisposable
{
    public void Dispose()         


        
4条回答
  •  隐瞒了意图╮
    2020-12-09 17:47

    Well of course the first thing to do is to actually have the Dispose method clean up the members of the collection. I'm assuming that's just an error in the example rather than the real code.

    Beyond that, I would just suppress the warning. I very strongly hold that any suppression:

    1. Should be of a very short scope, so it doesn't suppress another case of the warning that is a genuine mistake.
    2. Should be annotated with a comment, no matter how brain-dead obvious it seems that the warning is safe to suppress.

    That said, I think the analysis of CA2000 is so poor as to not be worth having it checked by default, but only in occasional reviews. After a certain number of false-positives a warning can't even be considered a warning any more, just noise that's hiding real warnings and hence making code more likely to be buggy.

提交回复
热议问题