Use the Obsolete attribute in C#

[obsolete attribute]

The Obsolete attribute tells the C# IDE that a method is outdated and should no longer be used. You can include a string to display to the developer. The string usually tells what newer method to use instead of the obsolete method. You can also determine whether the IDE treats using this method as a warning or an error.

In this example, the following 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 Obsolete attribute in the following method uses final parameter true to indicate that its use should raise an error and not be 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()
{
}


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in attributes and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *