harriyott.com

Disabling specific compiler warnings at specific points in code

Here's another of those useful-but-dangerous features that I stumble across from time to time. It is possible to disable a warning for a chunk of code, thus:

public bool UsedExternallyOnly = true;

would generate a warning:

warning CS0219: The variable 'UsedExternallyOnly' is assigned but its value is never used

There might be really good reasons for not fixing this warning, like it's only ever going to be used externally, such as in a derived class in a different assembly. Rather than pestering everyone by showing the warning everytime, or hiding valid warnings by switching them off for the project, the one offending line of code can be ignored for this warning:

#pragma warning disable 219
public bool UsedExternallyOnly = true;

#pragma warning restore 219

Marvellous.
8 March 2006