3

I have a warning on an event in a class that the event is never used. The class implements an interface as a null object and as such it is never supposed to use the event; the event only exists to implement the interface.

I tried following the accepted answer in this question, but it didn't do anything. The event is public and I'm using Visual Studio 2017 and Visual Studio for Mac (aka Xamarin, aka Monodevelop + extensions), in case that makes a difference.

Clearer
  • 2,166
  • 23
  • 38
  • 2
    Wrap a `#pragma warning disable 0067` and `#pragma warning restore 0067` around the event declaration. – poke Apr 24 '18 at 06:59

1 Answers1

10

The reason this warning is raised is because by default events will generate fields. This makes the object larger which is undesirable if you never raise the event. To work around this use explicit add/remove methods for the event and do nothing in them:

interface I { event EventHandler E; }

class C : I
{
    public event EventHandler E { add { } remove { } }
}
Mike Zboray
  • 39,828
  • 3
  • 90
  • 122