-2

Is anyone else seeing the 642 warning not being caught by
#pragma warning disable 642?

Thanks,


Also, why doesn't suppress work?

================================================================
To clarify, this is not: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0462
That is an error.

It is: https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0642
This is a WARNING

Lee Wood
  • 55
  • 7

1 Answers1

3

Note: the question originally showed a pragma disabling CS0462, not CS0642.

You can only disable warnings, not errors. CS0462 is an error, not a warning, hence you can't disable it. Basically this is a problem in your code that you need to fix rather than ignore. (I'd personally suggest fixing the code rather than suppressing warnings in almost all cases anyway, but at least there's the option for warnings.)

Now that the question has been changed to refer to CS0642, it looks like what had actually happened was that you'd got the wrong number in the pragma. This works fine for me - the warning is disabled:

using System;

class Test
{
    static void Main()
    {
#pragma warning disable 0642
        if (true);
#pragma warning restore 0642        
    }
}

So basically, when you're trying to disable a warning, make sure you're using the right number.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • warning CS0642: Possible mistaken empty statement Says warning not error. You actually made me go check that I didn't know how to read! I appreciate the coding advice (to fix warnings instead of suppress them, thanks) but it's not relevant here. This isn't a problem it is intentional. For instance: `if (true) ; // note the intentional semicolon.` – Lee Wood Apr 06 '18 at 22:47
  • @LeeWood: "Says warning not error." Yes, because you've changed the number in the question from 462, which is an error. (You had it incorrectly twice, and correctly once.) Changing the question to make it look like the mistake was made by myself and Ron Beyer, with emphasis on "this is a WARNING" as if we were stupid, is rude IMO. – Jon Skeet Apr 07 '18 at 07:31