Oboslete attribute in C#

gpeipman
932 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Oboslete attribute

Usually breaking changes in code are prepared through multiple releases to provide backward support for systems that are not so easy to change. To bring coming changes straight to IDE and compiler .NET Framework has Obsolete attribute that can be used to decorate code artifacts that will be or are replaced by something else.

Here is the example of obsolete method. Take a look at compiler output when running this demo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
public class SomeClass
{
public void SomeMethod()
{
}
[Obsolete]
public void SoonToBeRemovedMehtod()
{
}
}
class Hello
{
static void Main()
{
var myObject = new SomeClass();
myObject.SomeMethod();
myObject.SoonToBeRemovedMehtod();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Describing the change

The fact that method is obsolete is often not enough as developers want to know how to update their code. For this the obsolete attribute supports description that shortly communicates next steps to developers or points out where more guidance about coming change can be found.

Here is the example of obsolete attribute with decsription. Run the demo and check compiler output now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
public class SomeClass
{
public void SomeMethod()
{
}
[Obsolete("Please use SomeMethod() instead of SoonToBeRemovedMehtod()")]
public void SoonToBeRemovedMehtod()
{
}
}
class Hello
{
static void Main()
{
var myObject = new SomeClass();
myObject.SomeMethod();
myObject.SoonToBeRemovedMehtod();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

References

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content