How can you mark code as “not for future use”

后端 未结 4 2526
你的背包
你的背包 2021-02-19 17:04

I often end up in a situation where I want to discourage other developers from continuing to use a method or class. For example, let\'s say I have two library methods \"A\" and

4条回答
  •  难免孤独
    2021-02-19 17:40

    Adding the Obsolete attribute to your method will give the strikethrough in intellisense.

    [ObsoleteAttribute("This property is obsolete. Use NewProperty instead.", false)] 
    public static string OldProperty
    { get { return "The old property value."; } }
    

    To disable the warnings add this before the attribute:

    #pragma warning disable 612, 618
    

    And to reenable:

    #pragma warning restore 612, 618
    

    As noted here, putting a ignore in your project file instead of in your code would be a very clean solution.

    618
    

    EDIT: Also, check out @JonHanna's answer about using the EditorBrowsable attribute.

    As others have noted, there are actually 2 warnings that are thrown with the obsolete attribute.

    EDIT:

    #pragma warning disable 612, 618
    [Obsolete]
    #pragma warning restore 612, 618
    public class test1
    {...
    

    When you try to use test1 you will get:

    enter image description here

    Note that when you type var test = new test1() the strikethrough does not occur.

    But test1 test = new test1() will show the strikethrough.

提交回复
热议问题