Back
Close

Oboslete attribute in C#

gpeipman
1,490 views

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.

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.

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

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants