State Street Gang
.NET, straight up

Disable "The event ... is never used" compiler warnings

February 6, 2008 09:37 by will

I have need for this every once in awhile; now I know exactly where to look for the answer.

Compiler warnings are useful.  But sometimes they're annoying, too.  For instance, when you're working on a framework and creating events.  Unless you're using the event within the assembly, you'll encounter compiler warning CS0067, "The event 'event' is never used". 

These compiler warnings can be suppressed.  I'm not suggesting you should suppress them automatically, every time they crop up.  You should consider whether or not the event is useful.  Events aren't just a single line of code.  The compiler spends much time creating classes and generating code in the background.  So, please, before you suppress your compiler warnings, take a moment to consider if it is a good idea or not.

Once you have decided to kill the warning, you can instruct the compiler to suppress the warning using a #pragma compiler instruction to suppress the warning thusly:

#pragma warning disable 67
        public event EventHandler UselessEventLol;
#pragma warning restore 67

The same command can be used to suppress any compiler warning.  Its best to disable them prior to the line you wish to suppress and then immediately restore (why it isn't enable I don't know) afterwards.


Tags:
Categories: Debug | Tips
Actions: E-mail | Permalink | Comments (0) | Comment RSSRSS comment feed

Related posts

Comments are closed