.NET Framework Class Library
SuppressMessageAttribute Class

Note: This class is new in the .NET Framework version 2.0.

Suppresses reporting of a specific static analysis tool rule violation, allowing multiple suppressions on a single code artifact.

Namespace: System.Diagnostics.CodeAnalysis
Assembly: mscorlib (in mscorlib.dll)

Syntax

Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.All, Inherited:=False, AllowMultiple:=True)> _
<ConditionalAttribute("CODE_ANALYSIS")> _
Public NotInheritable Class SuppressMessageAttribute
    Inherits Attribute
Visual Basic (Usage)
Dim instance As SuppressMessageAttribute
C#
[AttributeUsageAttribute(AttributeTargets.All, Inherited=false, AllowMultiple=true)] 
[ConditionalAttribute("CODE_ANALYSIS")] 
public sealed class SuppressMessageAttribute : Attribute
C++
[AttributeUsageAttribute(AttributeTargets::All, Inherited=false, AllowMultiple=true)] 
[ConditionalAttribute(L"CODE_ANALYSIS")] 
public ref class SuppressMessageAttribute sealed : public Attribute
J#
/** @attribute AttributeUsageAttribute(AttributeTargets.All, Inherited=false, AllowMultiple=true) */ 
/** @attribute ConditionalAttribute("CODE_ANALYSIS") */ 
public final class SuppressMessageAttribute extends Attribute
JScript
AttributeUsageAttribute(AttributeTargets.All, Inherited=false, AllowMultiple=true) 
ConditionalAttribute("CODE_ANALYSIS") 
public final class SuppressMessageAttribute extends Attribute
Remarks

This attribute can be applied to any application element.

NoteNote

The ConditionalAttribute is applied to this class, specifying the preprocessing symbol "CODE_ANALYSIS" as the conditional symbol that determines whether the attribute call is included or omitted. If the symbol is defined, the attribute call is included; otherwise, the call is omitted.

Example

The following code example shows the use of the SuppressMessageAttribute attribute to suppress performance warning messages.

Visual Basic
#Const CODE_ANALYSIS = True
Imports System
Imports System.Diagnostics.CodeAnalysis



Class Library
    
    <SuppressMessage("Microsoft.Performance", "CA1801:AvoidUnusedParameters", MessageId:="isChecked"), _
     SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId:="fileIdentifier")> _
    Shared Sub FileNode(ByVal name As String, ByVal isChecked As Boolean)
        Dim fileIdentifier As String = name
        Dim fileName As String = name
        Dim version As String = String.Empty

    End Sub 'FileNode
End Class 'Library 
C#
#define CODE_ANALYSIS
using System;
using System.Diagnostics.CodeAnalysis;

namespace CodeAnalysisSample
{
    class Library
    {
        [SuppressMessage("Microsoft.Performance", "CA1801:AvoidUnusedParameters", MessageId = "isChecked")]
        [SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fileIdentifier")]
        static void FileNode(string name, bool isChecked)
        {
            string fileIdentifier = name;
            string fileName = name;
            string version = String.Empty;
        }

    }
}
Inheritance Hierarchy

System.Object
   System.Attribute
    System.Diagnostics.CodeAnalysis.SuppressMessageAttribute
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
See Also

Tags :


Community Content

David M. Kean - MSFT
Do not try and code these by hand

You should never need to code a SuppressMessageAttribute yourself; both Visual Studio and the external FxCop (http://go.microsoft.com/fwlink/?LinkId=48211) can automatically generate these for you.

  • In Visual Studio, right-click a Code Analysis warning in the Error List and choose Suppress Message(s).
  • In FxCop, right-click a warning and choose Copy As -> SuppressMessage.
Tags :

David M. Kean - MSFT
FAQ: Why does Visual Studio Code Analysis (FxCop) seemly ignore the SuppressMessageAttribute?

This is typically because the CODE_ANALYSIS symbol is not defined.

For more information, see the following post on the Visual Studio Code Analysis blog:
http://blogs.msdn.com/fxcop/archive/2006/03/23/559149.aspx.

Tags :

Page view tracker