If a certain piece of code is no longer relevant or needed, or if it’s no longer maintained, you can mark it as obsolete.
Use the [Obsolete] tag to mark the code. You can mark a function:
[Obsolete("Use GetAsync<T>(string url) instead")]
public async Task<T?> GetAsync<T>(string url, string parameters)
{
var client = _httpClientFactory.CreateClient("HttpClient");
var response = await client.GetAsync(url + parameters);
if (!response.IsSuccessStatusCode)
throw new Exception($"{response.StatusCode}: {response.ReasonPhrase}");
var responseJson = await response.Content.ReadFromJsonAsync<T>();;
return responseJson;
}
Or you can mark a class:
namespace CatFacts
{
[Obsolete("The code no longer supports weather forecasts")]
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}
}
When the code is marked as [Obsolete], you will receive a warning when calling the function or referencing the class:
![Calling code marked with [Obsolete] will give a warning.](http://briancaos.files.wordpress.com/2024/04/obsolete.png?w=1024)
You can also return an error if the code is called. Include a true Boolean in the attribute:
[Obsolete("Use GetAsync<T>(string url) instead", true)]
public async Task<T?> GetAsync<T>(string url, string parameters)
{
var client = _httpClientFactory.CreateClient("HttpClient");
var response = await client.GetAsync(url + parameters);
if (!response.IsSuccessStatusCode)
throw new Exception($"{response.StatusCode}: {response.ReasonPhrase}");
var responseJson = await response.Content.ReadFromJsonAsync<T>();;
return responseJson;
}
The compiler will now throw an error:
![[Obsolete("", true)] will return an error](http://briancaos.files.wordpress.com/2024/04/obsolete2.png?w=857)
SO WHAT IS THE DIFFERENCE BETWEEN DEPRECIATED, DEPRECATED AND OBSOLETE CODE?
Depreciated and Deprecated are the same thing. Technically, we don’t depreciate code, we deprecate it.
- Deprecated code is code that currently is supported, but in the future will be removed. It’s the way you ease you out of functionality that you wish to remove. But at the same time give users of your code time to adapt and find a new solution – preferably use a new method or class that you include at the same time the old code is deprecated.
- Obsolete code is code that is no longer supported, or is irrelevant. Calling obsolete code is at your own risk as the result is deemed undefined. Usually this means that a feature have been removed from your codebase, but the call is still there to allow existing code to compile.
WHAT IF I MESS UP THE 2 TERMS?
Don’t worry, even Microsoft have mixed the terms. Look at the example above. The attribute is called [Obsolete], but the compiler warning say “Deprecated“.
As long as you supply a good description along with the [Obsolete] attribute, everything will be fine.
MORE TO READ:
- ObsoleAttribute Class from Microsoft
- Deprecated vs. Depreciated vs. Obsolete in Software Development by Emmanuella Budu
- How to Mark Methods as Deprecated in C# from CodeMaze