Method Deprecation In C#


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. 
  1. public void adition()  
  2.        {  
  3.            // intially created method , consumed by developers   
  5.        }  
  7.        public void improvised_adition()  
  8.        {  
  9.            // imrovised the addition method , for example may code fine tuneing.  
  11.        }  

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.
  1.         [Obsolete]  // using attribute.
  2.         public void adition()  
  3.         {  
  4.             // intially created method , consumed by developers   
  6.         }  
Please check the below snapshot. We are getting a warning message from the compiler that this method is deprecated.
Method Deprecation In C# 

Do you think this is meaningful?

We can issue the proper message to the developer. Please check the below code modification.
  1. [Obsolete("adition method logic is re-wriitened , now onwords use improvised_adition")]  
  2.        public void adition()  
  3.        {  
  4.            // intially created method , consumed by developers   
  6.        }  
Now developers can understand why this method is deprecated. Please check the below snapshot.
Method Deprecation In C# 
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.
  1. [Obsolete("adition method logic is computed wrongly , now onwords please use improvised_adition",true)]  
Method Deprecation In C#


In this blog, we have discussed the importance of obsolete attribute and the method depreciation concept with a simple program.