C# - Obsolete Attribute Example

In this example, we are going to learn about the Obsolete attribute in C#.
Submitted by Nidhi, on October 31, 2020 [Last updated : March 23, 2023]

Obsolete Attribute

The Obsolete attribute is used to specify the method has been obsoleted. Here, we will demonstrate the example of Obsolete attribute.

C# program to demonstrate the example of Obsolete attribute

The source code to demonstrate the Obsolete attribute is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# - Obsolete Attribute Example.

using System;

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

Output

Hello World
Press any key to continue . . .

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.

The above code will generate a warning for the obsolete method and it will print "Hello World" on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.