39

I have used c# to create a First Project. I have many warning errors and all these warning errors are to be single Error(Internal compiler error. See the console log for more information.)

For Reducing the warning errors I used #pragma Warning disable . #pragma warning restore front and back of the problematic code.

I have doubt that in my final build I should leave that #pragma warning disable & restore as it is in the program; or do I need to remove that? e.g:

#pragma warning disable
if (Displayer.instance != null && CTR.Tore== "Keepit")
{
    Displayer.instance.SetFielderProfile (i);
}
#pragma warning restore

For final build do I need to remove that or not?

SaravanaKumar
  • 739
  • 1
  • 7
  • 17

3 Answers3

71

At the very least you should be specific about which warnings you've deliberately chosen to ignore. That way, if later maintenance introduces a 'new' warning/issue that you should be made aware of, the warning about the newly-introduced error won't be suppressed by your blanket pragma warning disable directive.

You can get the warning numbers pertaining to the build issues you've decided to ignore from the build Output window in Visual Studio. They're usually labelled "Warning CS0168...." or similar. In which case you can specifically target just the those error(s) you've decided to ignore like so:

#pragma warning disable 168, 3021

    //Your code that generates warnings CS0168 and CS3021 here

#pragma warning restore 168, 3021
Deleted
  • 900
  • 5
  • 9
  • 23
    +1. I'd add a recommendation that one always have a comment explaining why 0128 and 3021 don't apply here; partly because if you can't write a good reason, then you are just hiding a bug, partly because it's the difference to someone else looking at it between a reasonable decision by a developer, and a lazy cover-up. – Jon Hanna Jan 07 '14 at 02:58
  • 2
    `#pragma warning disable CS0168, CS3021` can be used too. – Soner from The Ottoman Empire Nov 20 '20 at 21:21
8

If it is code of any practical value you should not have any warning and compile with "warnings as error" setting with all warnings turned on.

Code you showing does not seem like it have any errors in itself. So I see no reasons why you need the pragma.

But it is really your call - your code and if no one need to use/look/pay for it - do whatever works for you.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 1
    I disagree. I've had, for example, classes that needed to inherit from certain interfaces. These interfaces sometimes had events which I didn't need for that particular implementation. Why not pragma the warnings about unused events away? – shai tibber Nov 08 '17 at 08:12
  • 1
    @shaitibber if you have good reason (and provide good inline comment) - it is perfectly fine to disable occasional warnings inline. Code in the question shows no signs that such pragma is needed... and I believe the case you've mentioned should not cause warnings as implementation of an interface can't be treated by compiler as "unused" - asking separate question about why you got warning may be good idea (make sure to search if there is already good answer about whatever warning you are getting so). – Alexei Levenkov Nov 08 '17 at 18:24
  • 1
    @AlexeiLevenkov - Thanks for your comment. First of all, I think that saying "If it is code of any practical value you should not have any warning" implies that this is true in all cases, not just the one in question. Secondly, the example I gave checks out and generates warning cs0067. You can easily test it yourself (make sure Warning level is set to 4) – shai tibber Nov 10 '17 at 21:13
0

Adapting @Deleted's answer for MSVC and C++. Testing in Visual Studio 2019. See Deleted's answer for C#. Intended for anyone who found their way here after a web search.

Disable warning(s) in C++ code:

#pragma warning(push)
#pragma warning(disable : 4267 4293)

    // Your code that generates warnings C4267 and C4293 here

#pragma warning(pop)
AlainD
  • 5,413
  • 6
  • 45
  • 99