234

I'm using the Obsolete attribute (as just suggested by fellow programmers) to show a warning if a certain method is used.

Is there a way to suppress the warning similar to CodeAnalysis' SuppressMessage at points where the use is justified?

This needs to work for [Obsolete("Some message")] which generates warning 618 and the plain [Obsolete] attribute with no message which generates warning 612.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Alex
  • 75,813
  • 86
  • 255
  • 348
  • In generated code, it looks like a tough one. https://stackoverflow.com/a/66653324/2157640 – Palec Apr 01 '22 at 10:57
  • In the link above, I posted an answer that works since VS 2019. I am hesitant to repost the relevant part here as this Q is tagged [tag:visual-studio-2008]. TL;DR is: If there is a file name pattern where you need to suppress the warnings, use EditorConfig. – Palec Apr 04 '22 at 07:27

4 Answers4

289

Use #pragma warning disable:

using System;

class Test
{
    [Obsolete("Message")]
    static void Foo(string x)
    {
    }

    static void Main(string[] args)
    {
#pragma warning disable 0618
        // This one is okay
        Foo("Good");
#pragma warning restore 0618

        // This call is bad
        Foo("Bad");
    }
}

Restore the warning afterwards so that you won't miss "bad" calls.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    This only works if I don't give a message with my Obsolete attribute. I have a message though like this: [Obsolete("This is why this shouldnt be used - use XYZ instead.")]. Once I put the message in, the pragma warning disable 612 stops working and I'm getting warnings regardless. Do I need another error ID instead maybe? – Alex Jun 09 '09 at 05:41
  • 3
    Found it - The correct warning number is 618 if there's a message in the Obsolete attribute. Thank you! – Alex Jun 09 '09 at 05:56
  • 2
    Goodo - I've adjusted my example to match this. – Jon Skeet Jun 09 '09 at 06:18
  • Is there some place where the warning numbers are listed? – George Mauer Nov 09 '10 at 17:14
  • 6
    @George: You can find them [here](http://msdn.microsoft.com/en-us/library/ms228296.aspx). – Jordão Mar 11 '11 at 16:31
  • 3
    See my [answer](http://stackoverflow.com/questions/968293/c-sharp-selectively-suppress-custom-obsolete-warnings#9751124) on how to get the warning/error number in the first place. – Aaron Thoma Mar 17 '12 at 15:18
  • What if you use the Error=true parameter in the Obsolete attribute? Can you still prevent the compiler error. I would like to clearly express that the method is not to be used, but I don't want to delete all my unit tests (yet) until the new test methods have similar tolerance/variety coverage. Using internalsvisibleto is possible, but more impactful on the code than just putting an obsolete attribute and pragma in the test project. I don't like test induced damage. – Tormod Aug 14 '19 at 09:25
  • 2
    @Tormod: No, I don't believe you can ignore an obsolete-with-error attribute. You could potentially use conditional compilation - introduce a new build configuration that doesn't have them as errors, and only run the unit tests there. Or (ugly) use reflection... – Jon Skeet Aug 14 '19 at 10:30
  • Could you update the code to disable for 0612 as well please in case some readers use plain `[Obsolete]` without a message. – Caltor Jun 29 '20 at 12:42
  • 1
    @Jordão Note also that Visual Studio editor automatically unindents `#pragma` directives. – Caltor Jun 29 '20 at 12:44
  • Earlier you had `0612`, but then it was changed to `0618` for `Obsolete` tags that had custom messages associated. For vanilla `Obsolete` tags though, it looks like you still need `0612`. Perhaps this answer can show examples of both and explain the difference. – Panzercrisis Nov 05 '20 at 19:08
  • 1
    @Panzercrisis: I'll see if I can find time to do that at some point. Not right now though. – Jon Skeet Nov 05 '20 at 19:16
149

The intent is to disable the warning for obsolete usage, regardless of whether the construct is marked with [Obsolete] or [Obsolete("Message")]. So use both CS0612 and CS0618:

#pragma warning disable 612, 618 

...

#pragma warning restore 612, 618 
Jordão
  • 55,340
  • 13
  • 112
  • 144
  • 3
    Also note that the warnings don't occur if the obsolete usage is in a class that is itself marked as obsolete. – redcalx Aug 12 '14 at 09:15
  • 3
    Is there a way to disable an error as well, in case `Obsolete` was marked as an error? – Shimmy Weitzhandler Jan 20 '15 at 04:03
  • 2
    @Shimmy: if it's marked as an error, then you must fix it, there's no way to suppress errors. It might be a syntax problem, post it as a question on this site, and you'll certainly get a good answer. – Jordão Jan 20 '15 at 10:42
21

Here's how to get the warning/error number in the first place:

  1. Rebuild your project.
  2. Go to the Output window.
  3. Look up the line of the warning/error you want to suppress.
    For example:
    C:\Users\Username\Documents\Visual Studio 2010\Projects\Projectname\Classname.cs(203,7): warning CS0162: Unreachable code detected
  4. Copy the number part after "CS".
  5. Then proceed as Jon Skeet says.

(Better always proceed as Jon Skeet says…)

Community
  • 1
  • 1
Aaron Thoma
  • 3,820
  • 1
  • 37
  • 34
9

You're looking for the #pragma warning disable directive

Essentially you add the following command above the call site in the .cs file.

#pragma warning disable 612
SomeMethodCall

612 is the error message ID for calling obsolete methods

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • This only works if I don't give a message with my Obsolete attribute. I have a message though like this: [Obsolete("This is why this shouldnt be used - use XYZ instead.")]. Once I put the message in, the pragma warning disable 612 stops working and I'm getting warnings regardless. Do I need another error ID instead maybe? – Alex Jun 09 '09 at 05:41
  • 2
    Found it - The correct warning number is 618 if there's a message in the Obsolete attribute. – Alex Jun 09 '09 at 05:56