60

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?

Svish
  • 152,914
  • 173
  • 462
  • 620

3 Answers3

63

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)

ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101
  • 7
    In summary: behavior not defined so we need restore – Julian May 18 '15 at 11:30
  • The "end of the source file" assumption **does not** apply to partial classes split across multiple files... – McGuireV10 Oct 13 '18 at 10:56
  • @McGuireV10 Do you mean the pragma will, or will not be in effect for partial classes in other files? – StackOverthrow Dec 03 '18 at 18:36
  • 2
    @TKK it would seem to be "undefined" because it depends on the order in which the compiler processes the files. It goes into effect whenever the compiler happens across it and stays in effect until explicitly disabled. End-of-file doesn't apply to partials. You definitely want to disable and re-enable within each file wherever you need it. – McGuireV10 Dec 03 '18 at 23:18
  • Link to "language specification" is outdated. – Pang Apr 19 '21 at 02:02
3

No, you'll find that the compiler will automatically restore any disabled warning once it's finished parsing a source file.

#pragma warning disable 649
struct MyInteropThing
{
    int a;
    int b;
}
#pragma warning restore 649

In the above example I've turned of warning CS00649 because I intend to use this struct in an unsafe manner. The compiler will not realize that I will be writing to memory that has this kind of layout so I'll want to ignore the warning:

Field 'field' is never assigned to, and will always have its default value 'value'

But I don't want the entire file to not be left unchecked.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
John Leidegren
  • 59,920
  • 20
  • 131
  • 152
  • What if the warning was already disabled by a command line argument? You will end up restoring what you should not to. – drowa Oct 27 '16 at 20:29
  • @drowa then is suppose you don't need the disable in the first place. To my knowledge there is nothing equivalent to C++ push/pop in the C# compiler. So it's doesn't get any better than this. – John Leidegren Oct 28 '16 at 11:43
3

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.

Nir
  • 29,306
  • 10
  • 67
  • 103
  • 2
    You gave one use case, and conclude that all other use cases are the same. Furthermore I think this should have been a comment instead of an answer. – Micha Wiedenmann Aug 17 '15 at 11:22
  • 1
    @MichaWiedenmann - I was trying to explain by example, I could have written "sometimes you have two warnings of the same type in the same file where the first is a false positive but the second a real mistake" - would that be a better answer? in my opinion no, people would claim there are no real world use cases (if they didn't stop reading at "false positive") – Nir Aug 17 '15 at 11:39