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
2009-11-18 21:54:15
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
2009-11-18 21:59:10
@Rick nice addition, thanks for contributing.
Chris Ballance
2009-11-18 22:01:10
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
2009-11-18 22:04:22