Why is the compiler saying a field is never used when it is?

B. Clay Shannon picture B. Clay Shannon · May 28, 2014 · Viewed 9.8k times · Source

Pursuing perfection so as to catch beaudaciousness (paraphrasing Vince Lombardi), I tried to rid a project of its sole warning, which was: c:\project\nrps\PoisonFoot\releases\6-4-0\hhs\frmPlatypus.cs(103,16): warning CS0169: The private field 'PDQClient.frmPlatypus.scanned' is never used

So I commented out that line:

private bool scanned = false;

...but then I got:

C:\Project\nrps\PoisonFoot\Releases\6-4-0\HHS\frmPlatypus.cs(3390): The name 'scanned' does not exist in the class or namespace 'PDQClient.frmPlatypus'

...in two places (in the same class where it's supposedly unnecessarily defined)

How was I able to hoodwink the compiler without even trying? How can I get rid of the warning without receiving an err?

Answer

Idan Arye picture Idan Arye · May 28, 2014

When csc(the C# compiler) says a field is never used, what it actually means is that you never read from the field. If you only write to a private member field the compiler deduces that the field is redundant. So, I'm gonna assume that in line 3390 of frmPlatypus.cs you are assigning to scanned, not reading from it, and that's why the compiler complains.

Also - line 3390? Really?