#118 – Disabling Specific Compiler Warnings

There are times when you knowingly want to include C# code in your program that generates a warning.

For example, the following code will generate a warning at compile time:

 static void Main(string[] args)
 {
     uint x = 0x1234;
 }

The warning appears in the Output window – CS0219: The variable ‘x’ is assigned but its value is never used.

If you’re aware of what this warning means, you don’t intend to change the source code to resolve it, and you want to no longer see the warning for this particular line, you can use the #pragma warning directive.  Place the directive immediately above the offending line and reference warning #219.

 static void Main(string[] args)
 {
#pragma warning disable 219
     uint x = 0x1234;
 }

You’ll no longer get warning #219 for this line when you compile.