C#idiomatic C#
ca1041fundamentals/code-analysis/quality-rules

Code analysis

CA1041: Provide ObsoleteAttribute message

Learn about code analysis rule CA1041: Provide ObsoleteAttribute message

CA1041 Design enabled by default in .NET 10: As suggestion
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

A type or member is marked by using a System.ObsoleteAttribute attribute that does not have its System.ObsoleteAttribute.Message property specified.

By default, this rule only looks at externally visible types and members, but this is configurable.

Rule description

System.ObsoleteAttribute is used to mark deprecated library types and members. Library consumers should avoid the use of any type or member that is marked obsolete. This is because it might not be supported and will eventually be removed from later versions of the library. When a type or member marked by using System.ObsoleteAttribute is compiled, the System.ObsoleteAttribute.Message property of the attribute is displayed. This gives the user information about the obsolete type or member. This information generally includes how long the obsolete type or member will be supported by the library designers and the preferred replacement to use.

How to fix violations

To fix a violation of this rule, add the message parameter to the System.ObsoleteAttribute constructor.

When to suppress warnings

Do not suppress a warning from this rule because the System.ObsoleteAttribute.Message property provides critical information about the obsolete type or member.

Configure code to analyze

Use the following option to configure which parts of your codebase to run this rule on.

You can configure this option for just this rule, for all rules it applies to, or for all rules in this category (Design) that it applies to. For more information, see Code quality rule configuration options.

Include specific API surfaces

You can configure which parts of your codebase to run this rule on, based on their accessibility, by setting the api_surface option. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

ini
dotnet_code_quality.CAXXXX.api_surface = private, internal

Example

The following example shows an obsolete member that has a correctly declared System.ObsoleteAttribute.

csharp
[ObsoleteAttribute("This property is obsolete and will be removed in a " +
"future version. Use the FullName property instead.", false)]
public string Name
{
    get => "Name";
}

sample · vbdocs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/ca1041-provide-obsoleteattribute-message_1.vb

vb
Imports System

Namespace ca1041

    Public Class ObsoleteAttributeOnMember

        <ObsoleteAttribute("This property is obsolete and will " &
             "be removed in a future version. Use the FirstName " &
             "and LastName properties instead.", False)>
        ReadOnly Property Name As String
            Get
                Return "Name"
            End Get
        End Property

        ReadOnly Property FirstName As String
            Get
                Return "FirstName"
            End Get
        End Property

        ReadOnly Property LastName As String
            Get
                Return "LastName"
            End Get
        End Property

    End Class

End Namespace

See also