Mark a class or method as deprecated in c#

Background

The attribute Obsolete is used to mark types and members of types that should no longer be used. the Obsolete attribute is found in the System namespace; which means you can specify the type as Obsolete or ObsoleteAttribute—the suffix “Attribute” is automatically added at compile-time. A class or function can be made deprecated in c# by using obsolete attribute.For class/function use this attribute at top of the class/function. You can call attribute constructor by two ways,

[System.Obsolete("WARNING_MESSAGE")]
[System.Obsolete("WARNING_MESSAGE",[show compiler error true|false])]

Below is the example that shows usage of both constructor,

[Obsolete("Method1 is deprecated, please use Method2 instead.")]
public string Method1()
{
//function body
}

Upon calling this function compiler wont produces warning or error.

[System.Obsolete("Class1 is deprecated, use class2"),true]
class Class1
{
public void Method() { //function body  }
}

Upon calling instantiating this class, compiler wont produces warning or error.

Advertisements

2 thoughts on “Mark a class or method as deprecated in c#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s