Is there any error suppression mechanism in C# similar to the PHP arroba?

Asked

Viewed 208 times

12

I’m studying C#, but I’m used to programming in PHP.

I know that in PHP it is possible to delete some error messages by putting the @ (arroba) at the beginning of the expression.

For example, if I wanted the variable $a did not issue the Erro Notice, because it doesn’t exist, I could just use a @.

echo $a; // Notice: undefined variable 'a'
echo @$a;// Sem erros

Of course it’s not one of the best practices, but I’m not really here to know what good practice is or not, but I want to learn C# and I want to learn everything!

There is a C# error message suppression engine in the same way as PHP?

  • 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").

  • @Ivanferrer this is VB.NET. Does not apply in C#.

1 answer

12


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.

  • 3

    It is always good to remember that suppressing Warnings is different from suppressing gambiarras

Browser other questions tagged

You are not signed in. Login or sign up in order to post.