Deprecated methods in C#
Categories: C
Tags:
A deprecated method is a method that is no more supported. For example, you wrote an API with a function that saves something on the disk but in the future versions of your program you want to support only Database saves. In this case you can deprecate the method that saves to disk for a lot of versions: the programmers are now warned from the compiler every time they use the function, and have the time to migrate the code.

If you want to deprecate a method in c#, you can simply add the Obsolete attribute in the function.
[Obsolete("This is a deprecated method.")]
public void DeprecatedMethod()
{
MessageBox.Show("I'm a deprevated method! XD");
}
If we declare a method as obsolote, the Intellisense will inform us that the method is deprecated every time we use the code completion on the method and the compilation will produce a Warning or even an Error if we declare the attribute with the true parameter at the end:
[Obsolete("You can't use this metod. Use NewMethod instead", true)]
public void OldMethod()
{ .... }
public void NewMethod()
{ .... }
This last case is usefull when we stop supporting the accused function.
P.S. If you instead want to write a depravated method, check this site.
Se sei interessato a questo post, potresti anche provare a leggere:
- No related posts
13 Jun 2007 dzamir
Thanks for this. Top in the google search “c# declare as deprecated” and exactly what I needed.