242

Possible Duplicate:
How do I mark a method as Obsolete/Deprecated? - C#

How do you mark a class as deprecated? I do not want to use a class any more in my project, but do not want to delete it before a period of 2 weeks.

2
  • 19
    Isn't it bad form to mark the earlier question as the duplicate?
    – noelicus
    Dec 12, 2013 at 13:52
  • 6
    We should mark the first question as Obsolete/Deprecated. So the question is now: How to mark a question as Deprecated? Jul 10, 2017 at 15:07

4 Answers 4

435

You need to use the [Obsolete] attribute.

Example:

[Obsolete("Not used any more", true)]
public class MyDeprecatedClass
{
    //...
}

The parameters are optional. The first parameter is for providing the reason it's obsolete, and the last one is to throw an error at compile time instead of a warning.

5
  • 2
    The [Obsolete("Not used anymore",true)] here suffers from two major flaws: 1. If the code is not used anymore it should be deleted 2. The explanation doesn't provide information of what to use/do instead.
    – tmaj
    Dec 5, 2017 at 2:13
  • 7
    @tymtam You are right, but you might want to give developers some time to migrate their code to your new implementation before pulling out the rug under their feet. It is common to mark a component as obsolete for 1-2 releases. Sep 5, 2018 at 8:57
  • 5
    One use case would be: Leave it at default (false) for "1 or 2 releases", mentioning that in the comment. Then change it to true at the appropriate time, so those who are still using it will now get errors in their code and be forced to change. [Obsolete("Will be deprecated December 12, 2018. Use xyz instead.")] then... [Obsolete("Method was deprecated December 12, 2018. Use xyz instead.", true)]. That way it's clear. Then at another date you remove it completely.
    – DrCJones
    Oct 24, 2018 at 14:50
  • 4
    @tymtam It is not wise to delete public API immedietaly as other people using it will be confused and surprised. You should delete code without marking obsoloete only the private parts, or only if you are the only developer working on or using the code. Jul 1, 2019 at 10:08
  • 'Wise' is a strong word here. Python changed the syntax of its print methods; sometimes breaking changes are OK. (Microsoft's azure libraries are a more current example). If class X is not used anymore I think I would want my code to break a compile time rather than pretend it's OK.
    – tmaj
    Jul 5, 2019 at 13:05
51

As per Doak's answer, but the attribute's second parameter should be set to false if you want the code to compile:

[Obsolete("Not used any more", false)]
public class MyDeprecatedClass
{
        //...
}

This will just throw warnings.

1
  • 4
    You can also leave the attribute off entirely (it defaults to false): [Obsolete("Not used anymore")]; personally I find this more readable without the boolean at the end.
    – Wolfgang
    Nov 4, 2015 at 18:06
23

The reason to not erase a class and deprecate instead is to adhere to some "politeness policies" when your code is an estabished API and then is consumed by third parties.

If you deprecate instead of erase, you give consumers a life cycle policy (e.g., maintenance and existence of the classes until version X.X) in order to allow them to plan a proper migration to your new API.

-10

If you are using version control I would recommend just deleting the class. There is no reason to have unused code around.

Version control will be a handy undo if you decide later that you want the class.

11
  • 5
    I'm guessing/assuming he wants to mark it deprecated in order to allow the code to still compile in the meantime while he removes all references to it in the codebase.
    – shsteimer
    Nov 24, 2008 at 15:44
  • 19
    I need to do it progressivly. I can't erase it from the project right now. Commenting a class or a method is hard to find it later... I do not want to forget about it. Deprecating a method has still is place I think.
    – Mister Dev
    Nov 24, 2008 at 15:47
  • 12
    Downvotes are part of life. I don't harbor bad feelings about it. (but thanks)
    – jjnguy
    Nov 24, 2008 at 15:54
  • 6
    If you're developing a public API, you don't have the option of deleting an obsolete method or class. You often have to give your customers several releases to code away their dependency on your code. Nov 24, 2008 at 16:29
  • 9
    If you have a very big project, you can't just delete a class that is being used. You have to slowly migrate code as budgets and time dictates. Marking a class as obsolete encourages any new development towards to replacement class. Deleting therefore isn't always useful. USe [Obsolete("Please start to use ReferenceListBusinessService", false)] so that warnings are thrown instead of errors.
    – Rebecca
    Oct 12, 2011 at 10:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.