Home »
.Net »
C# Programs
C# - Generate an Error for Obsolete Method Using Obsolete Attribute
Learn, how to generate an error for the obsolete method using the Obsolete attribute in C#?
Submitted by Nidhi, on October 31, 2020 [Last updated : March 23, 2023]
Here, we will create an obsolete method using the Obsolete attribute that will generate the error.
C# program to generate an error for the obsolete method using the Obsolete attribute
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.
C# Basic Programs »