Use the Obsolete attribute in C#

The Obsolete attribute tells the C# IDE that a method is obsolete. You can indicate a string to display to the developer (usually telling what newer method to use instead) and you can determine whether the IDE treats using this method as a warning or an error. In this example, the Allowed method is marked as obsolete but is allowed. The IDE flags the line calling the method and displays a warning in the Tasks pane, but it will compile and execute the program if it uses this method.
[Obsolete("This method will soon be discontinued. Use the new version instead.", false)]The Disallowed method is marked as obsolete and the final parameter to the attribute indicates that its use is not allowed. The IDE flags the line calling the method, displays an error message in the Tasks pane, and will not allow the program to run if this method is used.
private void Allowed()
{
}
[Obsolete("This method is no longer allowed. Use the new version instead.", true)]
private void Disallowed()
{
}


Comments