CA1041: Provide ObsoleteAttribute message

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

TypeNameProvideObsoleteAttributeMessage
CheckIdCA1041
CategoryMicrosoft.Design
Breaking ChangeNon-breaking

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

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 ObsoleteAttribute is compiled, the 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.

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

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

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

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";
            }
        }
    };
}

System.ObsoleteAttribute

Show: