Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 909
Colonel Panic Avatar asked Jan 04 '13 16:01

Colonel Panic


1 Answers

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.

like image 50
SLaks Avatar answered Oct 04 '22 06:10

SLaks