user34537
user34537

Reputation:

C# disable warning

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

Upvotes: 14

Views: 18322

Answers (3)

Manfred
Manfred

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

Razzie
Razzie

Reputation: 31222

Yes. See the docs for the complete reference.

Upvotes: 5

Anton Gogolev
Anton Gogolev

Reputation: 115751

Almost precisely the same directive.

Upvotes: 19

Related Questions