Is there an attribute like Obsolete
or Deprecated
to indicate that a method has become obsolete or deprecated in the next version of a C# API? Otherwise, is there a standard for indicating that, like adding it in the remarks of a method's documentation?
Asked
Active
Viewed 770 times
0

Hugo
- 349
- 3
- 6
- 23
-
Do you have any example? Why don't you simply use `Obsolete` giving the appropriate message? For example: `[Obsolete("This will be deprecated. Please use ...", false)]`. Notice that the second argument indicates if it's going to throw an error when you try to use it. – Mateo Jun 27 '22 at 15:40
-
@Mateo I used `Obsolete` in the new version, but I'd like to indicate to users still on the old version, which is still supported, that using that method will make updating harder – Hugo Jun 27 '22 at 15:44
-
If you need users on the old version to be notified, you either need to update the old version to tell them it will be obsoleted by adding the `Obsolete` attribute so that the compiler throws warnings when they are developing, or send out a company memo or some sort of code analysis that tells them which methods to avoid. Typically when things are marked as obsolete, they aren't removed for several code version iterations (if at all, at least for Microsoft) with the idea that they are still being used. – Ibrennan208 Jun 27 '22 at 15:56
1 Answers
4
You should mark the method as obsolete using the Obsolete annotation. Refer to ObsoleteAttribute for details.
[Obsolete("Your warning message")]
public void MyMethod()
{
// Method body
}
If you want to throw an error instead of a warning, you could set the second argument error which is a boolean to true.
[Obsolete("Your error message", true)]
public void MyMethod()
{
// Method body
}

Methran G S
- 156
- 3
-
So it makes sense to mark a method as obsolete without having a replacement method yet? – Hugo Jun 27 '22 at 15:59
-
Not really. When a method is marked Obsolete, it should have a replacement method else the functionality is considered to be removed. If you are planning to scrap the method, make sure to set error argument to true and notify the end user about the same. If it is the other case where you would want the end user to use the alternative method, notify the user about the new method in the obsolete warning message. – Methran G S Jun 27 '22 at 16:36
-
-
It does work in your case, you'll have to specify that the method is no longer relevant and will make things hard when updating. Also inform about the equivalent implementation (If available). – Methran G S Jun 27 '22 at 17:54