C#: Is pragma warning restore needed?

前端 未结 3 737
失恋的感觉
失恋的感觉 2020-12-11 00:35

From msdn I get this:

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

In the examples, both disable

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-11 00:38

    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.

提交回复
热议问题