This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
This article covers the various ways you can suppress warnings from code analysis when you build your .NET app. You can suppress code quality rules, code style rules, and third-party analyzer rules using the information provided here.
Tip
If you're using Visual Studio as your development environment, thelight bulb menu provides options that generate the code to suppress warnings for you. For more information, seeSuppress violations.
You can disable a rule that's causing a warning by setting its severity tonone in an EditorConfig or AnalyzerConfigconfiguration file. This action disables the rule for your entire file or project, depending on the scope of the configuration file that you use.
[*.{cs,vb}]dotnet_diagnostic.<rule-ID>.severity = noneFor more information about rule severities, seeConfigure rule severity.
Use a#pragma warning (C#) orDisable (Visual Basic) directive to suppress the warning for only a specific line of code.
try { ... } catch (Exception e) {#pragma warning disable CA2200 // Rethrow to preserve stack details throw e;#pragma warning restore CA2200 // Rethrow to preserve stack details } Try ... Catch e As Exception#Disable Warning CA2200 ' Rethrow to preserve stack details Throw e#Enable Warning CA2200 ' Rethrow to preserve stack details End TryYou can use aSuppressMessageAttribute to suppress a warning either in the source file or in a global suppressions file for the project (GlobalSuppressions.cs orGlobalSuppressions.vb). This attribute provides a way to suppress a warning in only certain parts of your project or file.
The two required, positional parameters for theSuppressMessageAttribute attribute are thecategory of the rule and therule ID. The following code snippet passes"Usage" and"CA2200:Rethrow to preserve stack details" for these parameters.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2200:Rethrow to preserve stack details", Justification = "Not production code.")]private static void IgnorableCharacters(){ try { ... } catch (Exception e) { throw e; }}If you add the attribute to the global suppressions file, youscope the suppression to the desired level, for example"member". You specify the API where the warning should be suppressed using theTarget property.
[assembly: SuppressMessage("Usage", "CA2200:Rethrow to preserve stack details", Justification = "Not production code.", Scope = "member", Target = "~M:MyApp.Program.IgnorableCharacters")]Use thedocumentation ID for the API you want to reference in theTarget attribute. For information about documentation IDs, seeDocumentation ID format.
To suppress warnings for compiler-generated code that doesn't map to explicitly provided user source, you must put the suppression attribute in a global suppressions file. For example, the following code suppresses a violation against a compiler-emitted constructor:
[module: SuppressMessage("Design", "CA1055:AbstractTypesDoNotHavePublicConstructors", Scope="member",)]Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?