tags:

views:

404

answers:

3

How do I mark a method as Obsolete/Deprecated using C# ?

+30  A: 

The shortest way is:

 [Obsolete]

You can add an explanation:

 [Obsolete("Method1 is deprecated, please use Method2 instead.")]

You can also cause the compilation to fail if the method is called from somewhere in code like this:

 [Obsolete("Method1 is deprecated, please use Method2 instead.", true)]

(Thanks @rick)

Chris Ballance
if you want the compiler to throw an error if somebody uses the method use the overloaded method Obsolete(String Message, Bool error)
Loki Stormbringer
@Rick nice addition, thanks for contributing.
Chris Ballance
+1  A: 
[Obsolete]
    public void MyMethod()
Mr Grok
Chris's version is more descriptive ... use that
Mr Grok
A: 

Add an annotation to the method using the keyword Obsolete. Message argument is optional but a good idea to communicate why the item is now obsolete and/or what to use instead. Example: [Obsolete("use myMethodB instead")]myMethodA()

jchadhowell