Introduction
In this blog, we are discussing how we can mark the method as deprecated, with the help of a simple program we can understand: method deprecation.
What is method deprecation?
I have created a console application with a simple class. In the class, I have two methods, those are addition and an improvised method of addition or the existing method. Now you want to tell your developer who is consuming the method addition instead of the addition method, and from now on it will consume improvised addition method.
- public void adition()
- {
-
-
- }
-
- public void improvised_adition()
- {
-
-
- }
Now the question is, how to say the method is deprecated?
It is very simple-- please use the obsolete attribute. Please check the below code sample.
- [Obsolete] // using attribute.
- public void adition()
- {
-
-
- }
Please check the below snapshot. We are getting a warning message from the compiler that this method is deprecated.
Do you think this is meaningful?
We can issue the proper message to the developer. Please check the below code modification.
- [Obsolete("adition method logic is re-wriitened , now onwords use improvised_adition")]
- public void adition()
- {
-
-
- }
Now developers can understand why this method is deprecated. Please check the below snapshot.
Now the situation is different -- instead of a warning message you want to throw an error or you want forcefully to ask your developers to change the method.
Please check the code sample and the output. Make the below change in your attribute.
- [Obsolete("adition method logic is computed wrongly , now onwords please use improvised_adition",true)]
Summary
In this blog, we have discussed the importance of obsolete attribute and the method depreciation concept with a simple program.