Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppressing "is never used" warning for events in C# [duplicate]

Tags:

c#

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.

like image 655
Clearer Avatar asked Sep 16 '25 09:09

Clearer


1 Answers

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 { } }
}
like image 163
Mike Zboray Avatar answered Sep 18 '25 23:09

Mike Zboray