223

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.

Community
  • 1
  • 1
Mister Dev
  • 9,031
  • 12
  • 39
  • 33

4 Answers4

397

You need to use the attribute [Obsolete].

This is an example:

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

You do not have use parameters, they are optional (overloaded method). The first parameter is for the reason and the last one is to mark an Error in compile time instead of a warning.

cja
  • 10,504
  • 18
  • 66
  • 120
Patrick Desjardins
  • 125,683
  • 80
  • 286
  • 335
  • 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. – tymtam Dec 05 '17 at 02:13
  • 3
    @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. – Michael Kargl Sep 05 '18 at 08:57
  • 2
    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 '18 at 14:50
  • 1
    @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. – Guney Ozsan Jul 01 '19 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. – tymtam Jul 05 '19 at 13:05
46

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.

cja
  • 10,504
  • 18
  • 66
  • 120
Rebecca
  • 12,888
  • 10
  • 84
  • 127
  • 2
    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 04 '15 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.

-8

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.

jjnguy
  • 128,890
  • 51
  • 289
  • 321
  • 4
    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 '08 at 15:44
  • I agree, Why not delete the class, or if you don't have version control, comment it? – configurator Nov 24 '08 at 15:45
  • That would be one great reason for not using my solution I guess. – jjnguy Nov 24 '08 at 15:45
  • 16
    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 '08 at 15:47
  • 2
    That makes sense. I would get rid of the class eventually though. – jjnguy Nov 24 '08 at 15:48
  • Yes it's my plan... read the question :P in 2 weeks :P – Mister Dev Nov 24 '08 at 15:50
  • 11
    Downvotes are part of life. I don't harbor bad feelings about it. (but thanks) – jjnguy Nov 24 '08 at 15:54
  • 5
    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. – Michael Meadows Nov 24 '08 at 16:29
  • I guess I'm not thinking big-picture – jjnguy Nov 24 '08 at 16:34
  • 6
    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 '11 at 10:56
  • @Junto, that is a great point. – jjnguy Oct 12 '11 at 13:31