Provide ObsoleteAttribute message

TypeName

ProvideObsoleteAttributeMessage

CheckId

CA1041

Category

Microsoft.Design

Breaking Change

NonBreaking

Cause

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

Rule Description

ObsoleteAttribute is used to mark deprecated library types and members. Library consumers should avoid using any type or member that is marked obsolete, because it might not be supported and will eventually be removed from later versions of the library. When a type or member marked with ObsoleteAttribute is compiled, the Message property of the attribute is displayed, providing the user with 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 ObsoleteAttribute constructor.

When to Exclude Warnings

Do not exclude a warning from this rule as the Message property provides critical information about the obsolete type or member.

Example

The following example shows an obsolete member with a properly declared ObsoleteAttribute.

Imports System

Namespace DesignLibrary

    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
using System;

namespace DesignLibrary
{
    public class ObsoleteAttributeOnMember
    {
        [ObsoleteAttribute("This property is obsolete and will " +
             "be removed in a future version. Use the FirstName " +
             "and LastName properties instead.", false)]
        public string Name
        {
            get
            {
                return "Name";
            }
        }

        public string FirstName
        {
            get
            {
                return "FirstName";
            }
        }

        public string LastName
        {
            get
            {
                return "LastName";
            }
        }

    }
}
using namespace System;

namespace DesignLibrary
{
    public ref class ObsoleteAttributeOnMember
    {
    public:
        [ObsoleteAttribute("This property is obsolete and will " 
            "be removed in a future version. Use the FirstName " 
            "and LastName properties instead.", false)]
        property String^ Name
        {
            String^ get()
            {
               return "Name";
            }
        }

        property String^ FirstName
        {
            String^ get()
            {
               return "FirstName";
            }
        }

        property String^ LastName
        {
            String^ get()
            {
               return "LastName";
            }
        }
    };
}

See Also

Reference

System.ObsoleteAttribute