C# program to create an obsolete method in a class

Here, we are going to learn how to create an obsolete method in a class in C#?
Submitted by Nidhi, on September 10, 2020

To create an obsolete method, we need to use the Obsolete attribute to generate a compile-time warning.

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.

//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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.