Tuesday, November 19, 2013

Suppressing Compiler Warnings

Sometimes there are warnings that are necessary, but they can clog up your compiler output or MSBuild output. There is a simple way to turn off the ones that need to be there. Simply find the codes that should be suppressed by looking at the compiler output. For example, below is a warning from MSBuild (these can also be found in the Error List panel in Visual Studio).
c:\<path to file with the warning>\SomePage.aspx.cs(1196): warning CS0219: The variable 'SomeString' is assigned but its value is never used [C:\<path to build file>\Build.xml]
Then you surround the code that is generating the warning and surround it with #pragma warning tags like below. You can suppress multiple warnings like in the example below.
#pragma warning disable 0649
#pragma warning disable 0219

// Put your code here that will generate the warnings.  

#pragma warning restore 0219
#pragma warning restore 0649

No comments: