Using the Obsolete Attribute in C#

This article is also available on the Microsoft TechNet Wiki.

The Obsolete Attribute marks elements like classes, methods, properties, fields, delegates, and many others within your code as deprecated or obsolete.The attribute is read at compile time and it is used to generate a warning or an error to the developer.

This attribute can help you if you have ever wanted to make sure programmers use newer versions of your methods. It also makes it easier when you are transitioning from older methods to newer ones. Marking an item as obsolete warns users that program elements will be removed in future versions of the code base.

Obsolete Attribute

The attribute is found in the System namespace. The Obsolete attribute decorates a program element by putting the word “Obsolete” above it inside square brackets. Since it is an attribute, you can use either Obsolete or ObsoleteAttribute.

You can use it without arguments and in which case it generates a generic compile-time warning. It can also take one or two optional parameters: Message and IsError.

The Message parameter is a string value with the deprecation message. The message should state that the program element is obsolete and, if possible, point them to the new element to be used.

The IsError parameter is a Boolean value that tells the compiler whether to generate an error or not. An error is generated when IsError is true. If it is set to false, or is not included, then only a warning is generated.

Code Sample

The following code sample shows how to decorate methods with the Obsolete attribute:


using System;

class Program
    {
        // Mark Method1 obsolete without a message.
        [ObsoleteAttribute]
        public static string Method1()
        {
            return "You have called Method1.";
        }

        // Mark Method2 obsolete with a warning message.
        [ObsoleteAttribute("This method will soon be deprecated. Use MethodNew instead.")]
        public static string Method2()
        {
            return "You have called Method2.";
        }

        // Mark Method3 obsolete with an error message.
        [ObsoleteAttribute("This method has been deprecated. Use MethodNew instead.", true)]
        public static string Method3()
        {
            return "You have called Method3.";
        }

        public static string MethodNew()
        {
            return "You have called MethodNew.";
        }

        public static void Main()
        {
            Console.WriteLine(Method1());
            Console.WriteLine();
            Console.WriteLine(Method2());
            Console.WriteLine();
            Console.WriteLine(Method3());
        }
    }

The messages generated in the Error List appear as:

Error List

The two warnings are shown, as is the requested error.

References

About Ken Cenerelli
I am a technical writer/programmer writer/content developer, .NET developer, Microsoft MVP, public speaker, blogger, and Microsoft Azure nerd. I blog about technology at kencenerelli.wordpress.com and am on Twitter via @KenCenerelli.

One Response to Using the Obsolete Attribute in C#

  1. Pingback: 2016 Year in Review | Ken Cenerelli

Leave a comment