Monday, September 5, 2011

How to Suppress Compiler Generated Warnings in Visual Studio?


Visual Studio displays list of warnings when you compile code if any. These warning won’t stop your application from executing. These warning are useful in preventing defects. Sometimes warnings are undetermined or not real. In huge applications, warnings are also considered to keep good quality of code. In such scenario you might need to suppress such unwanted warnings. We can suppress compiler warnings using ‘#pragma warning’ preprocessor directive.

public static void Main()
{
       string name = "Mitesh";
}









In above code, string variable name is declared but never used so above warning will appear. You can suppress this warning using ‘#pragma warning’ directive. Let’s have a look on below modified code.

public static void Main()
{
#pragma warning disable
        string name = "Mitesh";
#pragma warning resotre
}








In first example, one warning appears which is suppressed using ‘#pragma warning‘ directive in second example. You can also add warning code with #pragma warning directive to suppress particular warning.


See also 
How to use Keyword as an Identifier in C#

No comments:

Post a Comment