Saturday 10 December 2016

C# selectively disable warnings

Problem:

I would like to selectively disable C# warnings.

Solution:

The syntax to selectively disable warnings is:

#pragma warning disable <warning number or warning list>
   <code block where warning is to be ignored>
#pragma warning restore <warning number or warning list>

Example:

#pragma warning disable 0618
  MyNecessaryObsoleteFunctionCall();
#pragma warning restore 0618

BadObsoleteCallThatShouldProduceWarning();

Notes:

C# compiler warnings are generally there for good reason, so it's better practice to resolve them rather than hide them. However, in some situations willfully ignoring/acknowledging a warning might be necessary (e.g. if warnings block your team's build, but the code is temporarily required for some important reason).

References:

https://msdn.microsoft.com/en-ca/library/441722ys.aspx http://stackoverflow.com/questions/968293/c-sharp-selectively-suppress-custom-obsolete-warnings

No comments:

Post a Comment