#509 – Use #pragma warning Directive to Disable Compile-Time Warnings

You typically want to fix any issues raised by the compiler as warnings at compile-time.  For example, the compiler might warning you that you declare a variable, but never use it.  In this case, you’d likely remove the variable declaration entirely.

There are times, however, when the compiler issues a warning about something that you know about and have no intention of “fixing”.  In the example below, we’ve implemented a CanBarkChanged event because it is part of an interface we are implementing, but we never fire the event.  The compiler warns us that we never fire this event.

Since we have no intention of firing the CanBarkChanged event, we can disable warning #67 when compiling this line of code, using the #pragma warning disable and #pragma warning restore directives.  We will then no longer get warning #67 for this line of code.

 

Advertisements