13

Is there a way in code to disable certain warnings in C# alike #pragma warning(cmd: warningsNo) in c++?

  • 1
    See also [this question](http://stackoverflow.com/questions/526443/globally-suppress-c-sharp-compiler-warnings) for disabling a warning in an entire project (link for the benefit of future googlers) – Roman Starkov Mar 20 '12 at 18:02

3 Answers3

17

Almost precisely the same directive.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
4

To disable a warning you can use #pragma. For example, add the following line just before the code in question:

#pragma warning disable RCS1194

The preceding example disables the warning RCS1194 which is "Implement exception constructors."

To enable the warning again, add the following after the code in question:

#pragma warning restore RCS1194

If you have more than one warning, use a comma separated list. For example:

#pragma warning disable RCS1021, RCS1102, RCS1194

Although the documentation states that the prefix CS is optional, the prefix RCS is not optional. I believe this also applies for other prefixes. My recommendation would be to always use the prefix to be on the safe side.

Manfred
  • 5,320
  • 3
  • 35
  • 29
4

Yes. See the docs for the complete reference.

Razzie
  • 30,834
  • 11
  • 63
  • 78