It is sometimes necessary to disable and restore a compiler warning and this can be done in the C# programming language using the #pragma preprocessing directive.
#pragma is used to optionally provide additional contextual information to the compiler. Compiler warnings can be disabled and restored using the following syntax.
#pragma warning disable [warning list]
#pragma warning restore [warning list]
Using the #pragma to disable and restore the compiler warnings where necessary allows you to still have the project setting to treat all warnings as errors enabled.
In the following example we’re disabling the C# compiler warning CS0618 which occurs when the compiler encounters obsolete code.
#pragma warning disable 618
#pragma warning restore 618
In the development of frameworks it is necessary occasionally to obsolete types or methods and refer developers to other types or methods that should be used in their place.
In CodePlex.Diagnostics version 4.0, for example, the ExceptionProvider and LoggingProvider classes will be replaced with C# extension methods and as such are now adorned with the Obsolete attribute.
Within the unit test for these classes, which are still required for backwards compatibility, the compiler warning is disabled and restored as shown here.
Compiler warnings should only be disabled after careful consideration although on occasion there is a need to disable compiler warnings. When disabling compiler warnings you should always restore the warning after the code that would otherwise cause the warning and all warnings should be treated as errors within the project settings.
. Read the rest at Intel.com.