Thursday, September 2, 2010

Suppress C# Compiler Warnings

When you compile any c# program it may produce warning & Errors. If your code contains any errors, it means there is some serious issue. If your code contains warning means it is tolerable.

Compiler has option which treat warning as errors. In that case your build process fail if your code contains warning.

Some sample code for Warning
class MyProgram
{
      int variable1;
}

It generates warning :Code111: The field variable1 is never used .

Code111 is Error number. you can supress this warning using Pragma warning compiler directive.

class MyProgram
{
#pragma warning disable 111 //Here u can give comma separated list of warning numbers
int variable1;
}

If you want to re-enable warning you can use restore keyword
#pragma warning restore 111 //if used it again it shows the warning

Best practice is your code always have 0 errors and 0 warnings.

Happy Coding!!