29

The syntax for disabling warnings is as follows:

#pragma warning disable 414, 3021

Or, expressed more generally:

#pragma warning disable [CSV list of numeric codes]

Is there a list of these numeric codes and the description of the warning that they're suppressing? Much to my chagrin, I can't seem to locate it via Google.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Garrett
  • 1,750
  • 2
  • 16
  • 23
  • 1
    not #pragma warning disable 414, 3021 - this is wrong! -1 for I spent few minutes wondering why I cannot disable warning in the way you provided. The correct way is #pragma warning( warning-specifier : warning-number-list [; warning-specifier : warning-number-list...] ) . Not CSV and you HAVE TO HAVE parenthesis. – There is nothing we can do Dec 10 '10 at 18:11
  • 2
    @Thereisnothingwecando according to [MSDN](https://msdn.microsoft.com/en-us/library/441722ys.aspx) this is the correct format, without parenthesis -- from memory, I'm almost positive that I've used `#pragma warning disable` and `#pragma warning restore` this way. – BrainSlugs83 Apr 23 '15 at 00:07
  • 2
    In case anyone else reads this, BrainSlug83 is correct. The commas are *not* need in C#. I believe the syntax that "there is nothing we can do" mentions, requiring parenthesis, is the C++ syntax. – Mike U Nov 24 '15 at 16:53

4 Answers4

42

MSDN has a list of warning codes. Unfortunately, you have to click each link to view what the code actually means.

MiffTheFox
  • 21,302
  • 14
  • 69
  • 94
14

You shouldn't need a list. The compiler will tell you. If you get a compiler error that says "warning CS0168", then add 168 to the list (or, better yet, fix the code).

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 34
    The important point here being to look at the Output tab ("Build" in particular"), not the Error List. Or just use msbuild/csc. But +1; that's what I do ;-p – Marc Gravell Jun 02 '09 at 22:56
  • 24
    You could need a list. This doesnt help if you are looking at other peoples code with an uncommented pragma statement. You could remove the pragma and see but doesnt always mean you will get the same result. – Beeeaaar Jan 02 '13 at 07:38
  • 1
    @marcGravell thanx. i searched a lot in the Error List for a setting to display the code ... – Offler Jul 04 '13 at 07:34
  • 16
    Damn right you could need a list. You *could* be working with a massive legacy app crawling with these pragmas and a variety of error codes. (Why?!) I'm not taking them out and recompiling every single place just to find out what they mean. < /rant > – SWalters May 26 '14 at 17:38
  • 3
    I need the list -- I have code where other developers have disabled specific warnings, and I need to know what they are so I can prioritize what order to re-enable them in -- without having a good idea of what is being suppressed, I don't know what's being hidden from me, and what the most important thing to fix first is. – BrainSlugs83 Apr 23 '15 at 00:09
  • 1
    I know this is old, but I just wanted to note that using a "#pragma warning disable" directive isn't just a way to hide errors. Sometimes, there is legitimate code that the compiler doesn't understand what is going on and *thinks* there is an error. That said, it should be used sparingly, and only when absolutely necessary, and you are *absolutely* certain that the compiler warning is incorrect. – Mike U Nov 24 '15 at 16:49
  • 17
    I don't know if standards were lower in 2009 or what, but "you don't need an answer" isn't an answer. – MrLore Jan 28 '16 at 07:25
7

Look down the list of C# compiler errors and warnings to find the individual warning numbers.

Jeffrey Hantin
  • 35,734
  • 7
  • 75
  • 94
3

FYI -

If you're using Visual Studio 2008, you can get the code directly from the Error List by right-clicking the error, and selecting Show Error Help from the context menu. The Help window should pop up, and show you everything you ever wanted to know about the error a la the MSDN website.

Could save you a bit of time.

Syndog
  • 1,531
  • 4
  • 19
  • 27
  • 2
    You can also get the error code by looking at the Output tab, which eliminates the need to open Help which can sometimes take a while. – Scott Dorman Aug 30 '09 at 18:03
  • True fact, if you're into digging through output text searching for error codes. Each to his own, I guess. ;) – Syndog Aug 30 '09 at 18:05
  • 1
    funnily with this i mostly end up at pages like "We are sorry. The page you requested cannot be found." (http://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=DE-DE&k=k(MSBuild.ResolveComReference.ResolutionWarning);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true ) or http://msdn.microsoft.com/library/vstudio/dd393602(v=vs.110).aspx).aspx (so ... 4 people find it helpful to only see a general error page) The link is one of the functionalities i learned to not use. – Offler Jul 04 '13 at 07:39