How to mark a class or method as deprecated in C# ?

There are times when you want to mark a class or method as deprecated in your source codeĀ  so that you donor want any developers to use it any more in the project.

You can use the obsolete attribute to achieve it.

When you define a class obsolete , you have the option of specifying 2 parameters
ā€“ Message ā€“ This is the text that is displayed when the developer uses the class or method.
ā€“ Error ā€“ This property is to to indicate if a compiler error should be triggered when the developer uses the obsolete class or method.

How to mark a class or method as deprecated in C# ?

Below is a sample code snippet demonstrating the usage of the obsolete attribute for a class and a method.

[Obsolete("This class is not used any more",false)]
public class Movie
{
    public string MovieName { get; set; }
    public List<string> Artists { get; set; }
    [Obsolete("This method should not be used", true)]
    public void SetArtists(List<string> artists)
    {
        Artists = artists;
    }
}

When we try to access the class Movie (defined as obsolete) , you will be indicated that the method is obsolete as shown below.

image

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 ā€“ ā€˜Propertyā€™: abstract properties cannot have private accessors Reason for the Error Youā€™ll get this error...
This is a really simple one . Below is a simple example of an enum called ā€œDesignationā€ defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...