5

What is the scope of a C# compiler directive (such as #pragma)? Where should such a directive be stated, and where does it take effect?

The C# specification is somewhat vague on this point.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    This is not a duplicate. He's not asking about pragma warning, he's asking about pragma. Pragma checksum, for example, is not answered by the linked question – Mark Sowul Oct 14 '16 at 13:16
  • Answer: it depends on the command (it is an implementation-specific instruction to the compiler). For example to disable warnings, there is the documented command `#pragma warning disable ### ` The implementation of that is such that it takes effect for the remainder of the file, unless you issue another `#pragma` command to re-enable it `#pragma warning enable ### ` Meanwhile, `#pragma checksum` simply applies to the file specified. – Mark Sowul Oct 14 '16 at 13:17

1 Answers1

5

The first line in the documentation is quite clear:

#pragma gives the compiler special instructions for the compilation of the file in which it appears.

So it just concerns the file the directive is in. If you put in the directive half-ways it is applied to the lower half.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    Thanks - so actually not clear then :-) . Unless I have misunderstood you it doesn't apply to the file in which it appears, instead it applies from where it is introduced in the file to the end of the file, or (as I have just learned elsewhere) where it encounters a matching #pragma restore directive. Unless it is applied at the project level as a project property, in which case it applies to the entire project. – Neil Haughton Oct 14 '16 at 13:19
  • You are totally right. It applies to the file only, starting from the line it stands. This is different from C++ for example (as far as I know), which does apply pragma's over files. – Patrick Hofman Oct 14 '16 at 13:21