Is there a way in code to disable certain warnings in C# alike #pragma warning(cmd: warningsNo) in c++?
Asked
Active
Viewed 1.6k times
13
-
1See 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 Answers
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