#118 – Disabling Specific Compiler Warnings
October 13, 2010 2 Comments
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.
What’s the significance of 219? Is that the type of warning or is it referring to the order the warning appears in the IDE?
That’s the warning number, indicating the type of warning. In this case, it warns that you that you’ve assigned a value to the variable but never used it.