code-analysisfxcop

How to silence fx cop warning CS0067?


On a build server, I see some weird message. It doesn't say so, but I think it's from some software called 'fx cop'

Warning CS0067: The event 'SunGard.Adaptiv.AnalyticsEngine.UI.CommonControls.DisabledCommand.CanExecuteChanged' is never used

How can I silence this message? Without changing what my class does.

sealed class DisabledCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

I stumbled upon docs for System.Diagnostics.CodeAnalysis.SuppressMessageAttribute which sounds useful, but there aren't any examples for my warning.


Solution

  • If you need to create an event that is never raised, you should make a noop event:

    public EventHandler CanExecuteChanged {
        add { }
        remove { }
    }
    

    The compiler is complaining because a default ("field-like") event will create a hidden backing field to store the handlers. Since you never raise the event, that field just wastes memory.