Iris Classon
Iris Classon - In Love with Code

Stupid Question 85: What does the 'Obsolete' attribute in C# do?

[To celebrate my first year of programming I will ask a ‘stupid’ questions daily on my blog for a year, to make sure I learn at least 365 new things during my second year as a developer]

.

A simple question, a weekend question if you like :) I haven’t used this attribute and I haven’t seen it ‘in action’, but looking at some code today I noticed it for the first time. Although the name is rather self explanatory, I wanted to look up this attribute and find out what it does and what it is used for.

This attribute is used to mark members and or types as obsolete, often because they are deprecated. You are telling that something is not recommended to be used. When the the type or member is used it will give a compiler warning or an error. The attribute can be written just like this:
[Obsolete]
, but is is recommended that you give an explanation, like so:
[Obsolete(“Nonononono!! don’t use me!!”)]
Both will give just a warning. To throw an error instead you add another argument, a boolean set to true like so:
[Obsolete(“I give u error for using me muahahah”,true)]

Here is an example:

example obsolete attribute

Comments

Leave a comment below, or by email.
Mike Reynolds
11/18/2012 3:38:36 PM
After reading this, I now feel compelled to use MUAHAHAH in code -- perhaps as a name of a class or property.

Mike Reynolds 
TheCodeJunkie
11/18/2012 3:39:07 PM
It's used to deprecate members of your API. In essence you are telling people "Yeah, you shouldn't really be using this, use that other thing instead, because I will be removing it pretty soon", instead of just removing the member without any warning. It gives people time to migrate. 

Throwing an error instead of a warning is a way to forcefully get people to migrate immediately, the same way you would if you had just removed the member, but with the added difference that you also get to give them a friendly error message with instructions on how to resolve the error. Just removing the member gives you no clue how to resolve the breaking change that was introduced. 
Henrk
11/18/2012 3:40:06 PM
When you build a publicly available API it's a way to be gentle and let your users have time to update their code. Obsolete (false) means "Hey! Still possible, but time to update!". Obsolete (true) means "Yes, you have done nothing wrong. It used be this way, but not anymore. You must update your code!" - instead of just blowing up and your users don't really remember why they wrote code (called a method) that doesn't even exists. 
Steve Smith
11/18/2012 3:44:40 PM
It's a nice way to point developers at the new way to do something, when you're preparing them for a future breaking change.  These developers an be internal or external.  For instance, in any large codebase, it may take quite a while to update all of the code that uses a particular method that's being deprecated.  Marking it obsolete can be a good interim step toward fixing this.

Of course, it's generally a "best practice" to treat warnings as errors, so if your code is referencing any obsolete code, you wouldn't be able to compile at that point, so you'd be motivated to fix the resulting code sooner rather than later.  Having loads of warnings from obsolete statements that are being ignored is definitely a sign of poorly maintained code. 
James Curran
11/27/2012 9:13:37 AM
One thing that really should be stated loudly is the the message in the Obsolete should always mention the NEW api which the developer should use instead.   Just saying "stop using this" isn't helping anyone.

Normally, I phase in the use of the "treat as error" flag.  i.e, if v2 obsoletes a v1 method, then in v2 the flag is false.   In v3, however, I set the flag to true (and replace the body of the method with a NotImplementedException)

P.S.  I glad to see that "MUAHAHAH" translates between English & Swedish. 


Last modified on 2012-11-17

comments powered by Disqus