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)]
private void Allowed()
{
}

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.

[Obsolete("This method is no longer allowed. Use the new version instead.", true)]
private void Disallowed()
{
}

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.