Click here to Skip to main content
Click here to Skip to main content

How can we mark a method as deprecated in c#?

, 14 Jul 2013
Rate this:
Please Sign up or sign in to vote.
How can we mark a method as deprecated?

Many times you want to warn developers that some methods or classes should not be used as they are either replaced or going to be replaced with new versions. This is possible by using the “[Obsolete]” attribute.

In the below class we have a method called as “Method1”. Now let’s say you have created a better improvised version of “Method1” called as “NewMethod1”. You want to send an alert to all your developers who are consuming this class to use “NewMethod1” rather than using “Method1”.

So you can decorate “Method1” with the “[Obsolete]” attribute as shown in the below code.

public class Class1
{
    [Obsolete]
    public void Method1()
    {

    }

    public void NewMethod1()
    {
    }
}

Now if any programmer consumes the above class he will get a warning message as shown in the below figure.

In case you want to show some message to the developers you can pass the message in the “Obsolete” attribute as shown in the below code snippet.

[Obsolete("Please use NewMethod1")]
public void Method1()
{

}

If you want to be bit strict and do not developers to use that method, you can pass ‘true” to the “Obsolete” attribute as shown in the below code.

[Obsolete("Please use NewMethod1",true)]
public void Method1()
{

}

Now in case developers try to make a call to “Method1” they will get error and not just a simple warning.

Also have a look at the following practical demonstration on C# Obsolete video: -

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Comments and Discussions

 
GeneralMy vote of 5 Pinmemberdxs75315-Jul-13 1:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 15 Jul 2013
Article Copyright 2013 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid