Use “Obsolete” attributes to indicate Obsolete Methods

By | January 30, 2011

In this post I have explained how you can use Obsolete attributes to mark some methods which are not no longer in used or may be remove in future release of the class. During the development cycle we may need to change the name or declaration of certain methods which may be using by some other developers. In this case if you changed the name or declaration of that method, application may crash in different point of times as it’s being used by other developer in the application. In this case you can use System.ObsoleteAttributes class to generate compiler warning and indicate the method as Obsolete.

1

Let’s discuss this with an example, Suppose we are having a Student Class which have GetStudentInfo() method which returns the students information.

2

Now this class is being used by many other developer to get the student information.

3

If you want to change the name or declaration of GetStudentInfo() methods it may causes the application crash or application build error as this method is being used by many developers.

4

To overcome this problem, and maintain the compatibility you can use “Obsolete” attributes to the old methods and define a new method and show a proper message to other developers.

5

This changes won’t be causes for any code break or application error. Other developers who are already using the GetStudentInfo() method will get an Compiler warning “Use GetStudentDetails Instead of GetStudentInfo” .

6

And for any new developer who are going to consume the GetStudentInfo() method will get an tool tip message that “Use GetStudentDetails Instead of GetStudentInfo

7

Author: Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

3 thoughts on “Use “Obsolete” attributes to indicate Obsolete Methods

  1. Pingback: Dew Drop – January 31, 2011 | Alvin Ashcraft's Morning Dew

  2. mlb jerseys

    You made some good points there. I did a search on the topic and found most people will agree with your blog.

Comments are closed.