What is the ObsoleteAttribute Anyway?

by jmorris 22. June 2010 17:31

From time to time during the lifecycle of any API, certain methods or classes simple become obsolete; meaning "No longer useful" or "No longer relevant." Often times it is a warning to users of the API that the lifetime of the said method is "finite" and will or may be removed in the future.  It is also may be an indication that another type or method has been created which supersedes the functionality of the decorated subject.

The FCL even provides a special attribute for flagging methods and types as obsolete: System.ObsoleteAttribute. Here is the MSDN description of the attribute:

“Marks the program elements that are no longer in use. Marking an element as obsolete informs the users that the element will be removed in future versions of the product.

Typically it is added when a method or class must be changed in a way that backward compatibility may not be completely possible. After all, in general most developers try as hard as possible to maintain backwards compatibility; or at least try as hard as their level of comprehension of the API and its clients. Occasionally this isn’t possible, so the attribute is used to flag a clear direction to the developer: “DO NOT USE THIS CLASS, METHOD, ETC…USE CLASS/METHOD/TYPE X, Y, OR Z INSTEAD!!!”

For example, the following method has been flagged with the ObsoleteAttribute and a clear, description directive has been made to all users of the method:

For those that ignore that message and use the method in there code, the compiler will even issue a warning:

So as a plea to all developers, when you encounter the ObsoleteAttribute, please, please respect it and don't use methods or types in your code that are flagged with it Smile

Tags: ,


Jeff Morris

Tag cloud

Month List

Page List