How do I mark a method as obsolete or deprecated using C#?
-
3Not to forget there is also `[EditorBrowsable(EditorBrowsableState.Never)]` (https://stackoverflow.com/a/9086345/661933). Serves a slightly different purpose. – nawfal Mar 16 '18 at 10:34
5 Answers
The shortest way is by adding the ObsoleteAttribute
as an attribute to the method. Make sure to include an appropriate explanation:
[Obsolete("Method1 is deprecated, please use Method2 instead.")]
public void Method1()
{ … }
You can also cause the compilation to fail, treating the usage of the method as an error instead of warning, if the method is called from somewhere in code like this:
[Obsolete("Method1 is deprecated, please use Method2 instead.", true)]

- 607
- 15
- 23

- 33,810
- 26
- 104
- 151
-
111if you want the compiler to throw an error if somebody uses the method use the overloaded method Obsolete(String Message, Bool error) – HitLikeAHammer Nov 18 '09 at 21:59
-
163Obsolete without a description should be obsolete...http://notherdev.blogspot.com/2013/02/obsolete-should-be-obsolete.html – dotjoe Apr 22 '13 at 18:36
-
36In you examples the "Method1 is deprecated" part is rather redundant. By marking it as obsolete you are saying that it is indeed obsolete, so no need to restate it in the message. Especially since the resulting warning/error will read 'Method1' is obsolete: 'Method1 is deprecated, please use Method2 instead.' – irreal Dec 25 '15 at 07:54
-
16That's fine. I just put some example text there to show that you can add a more specific message if you would like to. – Chris Ballance Dec 25 '15 at 15:16
-
@HitLikeAHammer If we're aiming for compiler error, then why not change the method name and let compiler cry about it? What's the point of annotation? – akshay2000 Jun 29 '16 at 16:09
-
19@akshay2000 Renaming or removing the method would leave the consumer clueless about why it was renamed or removed and what should be used instead. – Lensflare Jul 07 '16 at 09:25
-
3Another reason for forcing a compilation error on an obsolete method or property would be when it's needed for something that uses reflection but isn't intended for direct calls. The example I have in front of me is a property that's needed for serializing to a specific database but should never be used by application code. – E-Riz Apr 17 '17 at 14:16
-
3`Obsolete` allows you to gradually retire a method, class etc over a number of releases if you wish: warning, error, gone. That's kinder to the consuming code/coder, I think, than just removing the element in question. – SteveCinq Aug 01 '18 at 17:21
-
9It might be a good practice to use `[Obsolete("Method1 is deprecated, please use " + nameof(Method2) + " instead.")]` to avoid hard-coded strings, in case the method name has to change, for any reason. – Mladen B. Jan 25 '19 at 16:28
To mark as obsolete with a warning:
[Obsolete]
private static void SomeMethod()
You get a warning when you use it:
And with IntelliSense:
If you want a message:
[Obsolete("My message")]
private static void SomeMethod()
Here's the IntelliSense tool tip:
Finally if you want the usage to be flagged as an error:
[Obsolete("My message", true)]
private static void SomeMethod()
When used this is what you get:
Note: Use the message to tell people what they should use instead, not why it is obsolete.
With ObsoleteAttribute
you can mark a method as deprecated.
It has three constructors:
[Obsolete]:
is a no parameter constructor and is a default using this attribute.[Obsolete(string message)]:
in this format you can getmessage
of why this method is deprecated.[Obsolete(string message, bool error)]:
in this format message is very explicit buterror
means, in compilation time, compiler must be showing error and cause to fail compiling or not.

- 9,564
- 146
- 81
- 122

- 3,044
- 1
- 23
- 30
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:
[System.Obsolete("use myMethodB instead")]
void myMethodA()

- 16,580
- 5
- 54
- 111

- 1,131
- 1
- 14
- 25
For dependency injected methods, Apply the [Obsolete("description")]
attribute to the declaration rather than implementation (:doh: moment for me)

- 2,104
- 2
- 27
- 29