There is, it’s the #pragma warning
. Some considerations must be made.
This only exists at build time since this is a build directive. Nothing will change in code execution.
C# has the philosophy of relatively few warnings (now increased a little), the error is preferred or works without restrictions. The language was designed for this.
Its use should be extremely rare. Most codes will never have this.
It is useful when doing something normally strange that can really give a problem and has no other way to solve via code. I mean, you know really that there is no problem in that situation and need to report it to the compiler. A Warning is not an error, the compiler knows that the programmer can be right in rare circumstances.
No one puts it out of thin air, in general one realizes that it is necessary after seeing the code compiling and showing a Warning specific. After much thinking and being sure that there is no other way, then you can disable specifically that Warning through your code. So if one day appears another Warning in a maintenance, it will be presented. Never disable all warnings (not specifying the number(s) (s) of the) Warning(s)).
Its effect is valid for a block of code until it is turned off:
#pragma warning disable 168 //desabilitei aqui por causa de....
... código ...
#pragma warning restore
There is the recommendation to always comment on why this is used. If you can’t give a good reason, there is something wrong there. It’s not worth blaming the compiler :)
There are other #pragma
to give other types of instructions to the compiler. One of them is the #pragma checksum
.
There is also the #warning
which is used for your code to generate a Warning for the compiler. This should probably be used conditionally. Or something that should no longer be used (although there is another solution for this). Rare use, as every compilation directive should be.
Has several compilation directives which affect the functioning of the compilation and has a secondary effect on the code.
Although it is a bad practice to use @ to suppress errors, in C#, it is also possible to do something like:
On Error Resume Next "ignored error" System("pause")
.– Ivan Ferrer
@Ivanferrer this is VB.NET. Does not apply in C#.
– CypherPotato