47

Is there a way to suppress warnings in C# similar to Java's @SuppressWarnings annotation?

Failing that, is there another way to suppress warnings in Visual Studio?

Nosrama
  • 14,530
  • 13
  • 49
  • 58

8 Answers8

74

Yes.

For disabling, use:

#pragma warning disable 0169, 0414, anyothernumber

Where the numbers are the identifiers of the warnings that you can read from compiler output.

To reenable the warnings after a particular part of code (which is a good idea) use:

#pragma warning restore 0169, anythingelse

This way you can make the compiler output clean, and keep yourself safe because the warnings will only be suppressed for that particular part of code (where you made sure you don't need to see them).

Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
  • I'm not sure if the numbers are interpreted here correctly. Leading zero is parsed as 'octal' in general. So it might be safe to skip it. (not tried though) – fmuecke May 08 '14 at 12:51
  • 1
    @fmuecke Seems to work either way (in the mono compiler at least.) What you say is true for literals in the C# parser, but I guess the preprocessor parses number literals differently. – Eliot Nov 14 '15 at 17:34
  • @TamásSzelei This only works for SonarLint locally. I'm still seeing these errors reported in the SonarQube server results. – ConfusedDeer Feb 22 '17 at 18:42
  • 1
    @ConfusedDeer The answer refers to how the C# compiler can be instructed to silence the warnings locally. You should either use compiler flags to silence the warnings globally or consult the sonarlint docs. – Tamás Szelei Feb 23 '17 at 21:24
  • how to disable them all? – john k Mar 19 '23 at 16:36
20

Yes there is you can use the pragma warning annotation like this:

#pragma warning disable 414
//some code that generates a warning
#pragma warning restore 414

omitting the numbers disables and restores all warning codes...

Robban
  • 6,729
  • 2
  • 39
  • 47
7

I highly recommend using the following form

#pragma warning disable 649 // Field 'field' is never assigned to, and will always have its default value 'value'

#pragma warning restore 649

The comment on the first line is taken from the first like of the MSDN documentation for Compiler Warning (level 4) CS0649. Since warnings are numbered in C#, this is your only reference to what's actually going on in the code when you see a warning disabled. Placing it at the end of the line is the only way to get the reason to show up in the search results window when you do a search in your whole solution for pragma warning.

You can identify the warning numbers by looking in the Output window after building your project. Make sure it says Show output from: Build.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
6

There is. See the MSDN page on how to suppress compiler warnings.

From Visual Studio, go to your project properties, select the build tab, and enter the warning number in the Suppress Warnings field.

From code, to disable specific warnings, you can use the #pragma directive:

public class MyClass
{
  #pragma warning disable 0168
  // code

  // optionally, restore warnings again
  #pragma warning restore 0168
  // more code
}
Razzie
  • 30,834
  • 11
  • 63
  • 78
3

You could check #pragma directives: http://msdn.microsoft.com/en-us/library/441722ys(VS.80).aspx.

Marcin Deptuła
  • 11,789
  • 2
  • 33
  • 41
  • 1
    The link is Dead. This is why you should never answer questions with a link, always cut and paste the pertinent information from the link. – Paul Gorbas Aug 26 '19 at 17:52
3

Have a look at the SuppressMessageAttribute in VisualStudio: http://msdn.microsoft.com/en-us/library/ms182068.aspx

HerpDerpington
  • 3,751
  • 4
  • 27
  • 43
3

You can use the SuppressMessage data annotation which will prevent the warning.

It looks something like this:

[SuppressMessage("Reason #Enter whatever you'd like", "ID, must match what intellsense is showing it looks something like this: IDE0001", Justification = "(optional, your own description")]

Here is a real world example:

[SuppressMessage("IntelliSenseCorrection", "IDE0001", Justification = "Do Not Remove <T> Variable, It's Required For Dapper")]
TroySteven
  • 4,885
  • 4
  • 32
  • 50
0

I guess you could also try to review the project or solution properties and set your warning level to a lower level or so. Otherwise, the other responses are perhaps better.

Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162