How To Make A Method Obsolete In C#

What is Obsolete Keyword?

If we do not want developers to use a certain method then we can declare the method as obsolete. This keyword is used to deprecate a method. One strong reason for doing this could be new version of the method being developed or introduced. Using this keyword we can give a custom message to the user also.

How can I make a method Obsolete?

We can make a method obsolete by putting [Obsolete] attribute on the top of the method. Let us create a program and try to understand more on how to make a method Obsolete in C#.

Write the below code in a console application.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ObsoleteMethod  
  8. {  
  9.     class Program  
  10.     {  
  11.         [Obsolete("This method is Old. Kindly do not use!!")]  
  12.         public int AddNumber(int a, int b)  
  13.         {  
  14.             return a + b;  
  15.         }  
  16.   
  17.         public int Add(int a, int b)  
  18.         {  
  19.             return a + b;  
  20.         }  
  21.   
  22.         static void Main(string[] args)  
  23.         {  
  24.             Program obj = new Program();  
  25.   
  26.             int result = obj.AddNumber(12, 14);  
  27.   
  28.             Console.WriteLine(result);  
  29.   
  30.             Console.ReadLine();  
  31.         }  
  32.     }  
  33. }  
In the above program we have defined two methods. First method is declared obsolete with a error message. Second method is the newer version. When we declare a method as obsolete it gives a warning. Let us see the message.

CODE

When we try to call the obsolete method we get the same error message defined in the obsolete tag. If we try to run the program it runs and gives us the correct result. Suppose if we want developer to not even use this function then we can use an overloaded version of this keyword by passing a boolean value and setting it to true.

Check the modified version of the code below.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace ObsoleteMethod  
  8. {  
  9.     class Program  
  10.     {  
  11.         [Obsolete("This method is Old. Kindly do not use!!"true)]  
  12.         public int AddNumber(int a, int b)  
  13.         {  
  14.             return a + b;  
  15.         }  
  16.   
  17.         public int Add(int a, int b)  
  18.         {  
  19.             return a + b;  
  20.         }  
  21.   
  22.         static void Main(string[] args)  
  23.         {  
  24.             Programobj = newProgram();  
  25.   
  26.             int result = obj.AddNumber(12, 14);  
  27.   
  28.             Console.WriteLine(result);  
  29.   
  30.             Console.ReadLine();  
  31.         }  
  32.     }  
  33. }  
Just after the error message in the Obsolete tag I have placed a true.Let us see what happens now when we try to run the program.

ERROR

Now we are not able to even run the program as we get the run time error. So this is how we can make a method Obsolete in C#. 

 

Ebook Download
View all
Learn
View all