Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Is pragma warning restore needed?

From msdn I get this:

#pragma warning disable warning-list #pragma warning restore warning-list 

In the examples, both disable and restore are used. Is it necessary to restore if I want it disabled for a whole file?

Like, if I do not restore, how far does it carry? Are the warnings disabled for everything compiled after that? Or just for the rest of that file? Or is it ignored?

like image 980
Svish Avatar asked Feb 17 '09 13:02

Svish


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

If you do not restore the disabling is active for the remainder of the file.

Interestingly this behaviour is not defined in the language specification. (see section 9.5.8) However the 9.5.1 section on Conditional compilation symbols does indicate this "until end of file behaviour"

The symbol remains defined until a #undef directive for that same symbol is processed, or until the end of the source file is reached.

Given the 'pre-processor' is actually part of the lexical analysis phase of compilation it is likely that this behaviour is an effective contract for Microsoft's and all other implementations for the foreseeable future (especially since the alternate would be hugely complex and non deterministic based on source file compilation order)

like image 50
ShuggyCoUk Avatar answered Oct 07 '22 21:10

ShuggyCoUk


Let's say I have a private field that is initialized using reflections, the compiler obviously can't find any code directly writing into this field so it will show a warning - that I don't want to show.

Let's also say I have another private field defined 3 lines below the first that I forgot to initialize, if I disable the warning for the entire file this will not trigger a warning.

So, the best usage for #pragma warning is to put a "warning disable" right before the line that causes the warning that I want to suppress and a "warning restore" right after the line so the same condition in a different location in the file will still trigger a warning.

like image 30
Nir Avatar answered Oct 07 '22 20:10

Nir