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

Advertisements

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

2 Responses to #118 – Disabling Specific Compiler Warnings

  1. voxstudios says:

    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?

    • Sean says:

      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.

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 )

Google+ photo

You are commenting using your Google+ 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: