Is there a C # error suppression mechanism similar to that of PHP arroba?

8

I'm studying C #, however I'm used to programming in PHP.

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

For example, if I wanted the variable $a not to emit Erro Notice because it did not exist, I could simply 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 actually not here to know what is good practice or not, but I want to learn C # and want to learn everything!

Is there a mechanism for suppressing error messages in C # in the same way as PHP?

    
asked by anonymous 07.06.2016 / 14:19

1 answer

9

There is, indeed, #pragma warning . Some considerations should be made.

This only exists at compile time since this is a compilation directive. Nothing will change in the code execution.

C # has the philosophy of relatively few warnings , it prefers the error or works without restrictions. The language was meant for this.

Its use must be extremely rare. Most codes will never have this.

It's useful when you do something usually strange that really can give a problem and has no other way to solve via code. That is, you know really that you have no problem in that situation and need information to the compiler. A warning is not an error, the compiler knows that the programmer may be right in rare circumstances.

Nobody puts it out of nowhere. In general it is realized that it is necessary after seeing the code compiling and showing a specific warning . After much thought and making sure you have no other way, you can specifically disable this warning through your code. So if one day another warning appears in a maintenance, it will be displayed. Never disable all warnings (not specifying the warning number (s) ).

Its effect is worth by 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 a recommendation to always comment on why this is used. If you can not give a good reason, something is 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 #pragma checksum .

There is also #warning that is used for your code to generate a warning to the compiler. This should probably be used conditionally. Or something that should no longer be used (although there is another solution to this). Rare use, as any compilation directive should be.

You have multiple build directives that affect the workings of the build and have a side effect on the build. code.

    
07.06.2016 / 14:33