#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.

 

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

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

  1. Pingback: #1,127 – Where to Find Compiler Warning Numbers | 2,000 Things You Should Know About C#

  2. Pingback: #1,128 – Disable a Compile-Time Warning for Entire Project | 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: