The source code to create an obsolete method in a C# class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# Program to create an Obsolete method in a class
using System;
class Sample
{
[Obsolete("Use Display() method")]
static void Print()
{
}
static void Display()
{
Console.WriteLine("Print() method is obsolete, Use Display method in place of Print() method");
}
static void Main()
{
Print();
Display();
}
}
Output:
Print() method is obsolete, Use Display method in place of Print() method
Press any key to continue . . .
Explanation:
In the above program, we created a Sample class that contains three methods Print(), Display(), and Main().
Here we used the Obsolete attribute to obsolete a method with a specified warning message. Then we called both Print() and Display() method in the Main() method.
Program:
The source code to create an obsolete method in a C# class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created a Sample class that contains three methods Print(), Display(), and Main().
Here we used the Obsolete attribute to obsolete a method with a specified warning message. Then we called both Print() and Display() method in the Main() method.