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

Leave a Reply