I've been studying several source codes of WPF applications and seeing the SupressMessage attribute all over the place. The description of the attribute is very blurry to me right now. "Suppresses reporting of a specific static analysis tool rule violation, allowing multiple suppressions on a single code artifact." What is this attribute for and it's practical uses?
1 Answers
When you are applying code analysis to your project you will sometimes get warnings that you want to ignore either because the warning is a false positive or because it is OK for a specific part of your code to violate a code analysis rule.
You do that by using the SuppressMessageAttribute. Now code analysis is built into the "higher" SKU's of Visual Studio but previously you had to use the tool named FxCop to perform the analysis.
In general you have three options when you want to ignore a warning:
You can remove the rule from the ruleset that you are using to analyze the code. You should only do this if you really do not care about the rule.
You can suppress the warning in a project-wide suppression file normally named
GlobalSuppressions.cs
by using theSuppressMessageAttribute
. This will allow you to maintain all the suppressions in a single file but makes it somewhat hard to associate a specific suppression with a piece of code.You can suppress the warning in the source file where it occurs using the
SuppressMessageAttribute
. This creates a clear link between the code and the suppression but also litters the code with extra information. Note that some warnings can only be suppressed in the global suppression file because they are not associated with a specific piece of code.
The last two options are available directly in Visual Studio when you click on the Action dropdown on a code analysis warning. When you ignore a warning using the SuppressMessageAttribute
you can provide a value for Justification
. Doing that will allow you and also other developers at later point in time to understand why the warning was suppressed.
If you get warnings about spelling because you have some special words or abbreviations in your code you should probably not suppress the warning and instead create a custom code analysis dictionary for your project.
Using code analysis on your code will not only increase the quality of your code but you might also learn a few things in the process.

- 104,481
- 22
- 209
- 256