C# program to generate an error for the obsolete method using the Obsolete attribute

Here, we are going to learn how to generate an error for the obsolete method using the Obsolete attribute in C#?
Submitted by Nidhi, on October 31, 2020

Here, we will demonstrate the Obsolete attribute. Here, we created an obsolete method using the Obsolete attribute that will generate the error.

Program:

The source code to generate an error for the obsolete method using the Obsolete attribute is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to generate an error on the 
//obsolete method using the Obsolete attribute.

using System;

class Program
{
    [Obsolete("Absoluted method",true)]
    public static void SayHello()
    {
        Console.WriteLine("Hello World");
    }
    public static void Main()
    {
        SayHello();
    }
}

Output:

Error	1	'Program.SayHello()' is obsolete: 'Absoluted method'

Explanation:

In the above program, we created a class Program that contains two static methods, here we used the Obsolete attribute to specify the SayHello() method has been obsoleted and then called the method inside the Main() method.

    public static void Main()
    {
        SayHello();
    }

The above code will generate an error for the obsolete method SayHello() because we passed "true" with the Obsolete attribute then it will generate the error.





Comments and Discussions




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.