Is there a way to suppress warnings in C# similar to Java's @SuppressWarnings annotation?

后端 未结 8 926
北恋
北恋 2020-12-03 16:59

Is there a way to suppress warnings in C# similar to Java\'s @SuppressWarnings annotation?

Failing that, is there another way to suppress warnings in Visual Studio?<

相关标签:
8条回答
  • 2020-12-03 17:33

    I highly recommend using the following form

    #pragma warning disable 649 // Field 'field' is never assigned to, and will always have its default value 'value'
    
    #pragma warning restore 649
    

    The comment on the first line is taken from the first like of the MSDN documentation for Compiler Warning (level 4) CS0649. Since warnings are numbered in C#, this is your only reference to what's actually going on in the code when you see a warning disabled. Placing it at the end of the line is the only way to get the reason to show up in the search results window when you do a search in your whole solution for pragma warning.

    You can identify the warning numbers by looking in the Output window after building your project. Make sure it says Show output from: Build.

    0 讨论(0)
  • 2020-12-03 17:34

    Yes there is you can use the pragma warning annotation like this:

    #pragma warning disable 414
    //some code that generates a warning
    #pragma warning restore 414
    

    omitting the numbers disables and restores all warning codes...

    0 讨论(0)
提交回复
热议问题