Cloud Training (Online)

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.


Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.