Reputation:
Is there a way in code to disable certain warnings in C# alike #pragma warning(cmd: warningsNo) in c++?
Upvotes: 14
Views: 18322
Reputation: 5666
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.
Upvotes: 5