views:

201

answers:

2

Hi,

I need to suppress a specfic compiler warning in C#. Now I can do it like this:

#pragma warning disable 0649

private string _field;

#pragma warning restore 0649

Is there a way to do it like the following?

[SuppressCompilerWarning("0649")]
private string _field;

Because I only need to suppress warnings for this field, not a code block.

Note: I want to suppress the compiler warning, not the Code-Analysis warning.

Thanks!

+2  A: 

No. You can do it project wide via a build flag, but otherwise a field is just another (small) block.

Of course, you could assign it a value somewhere... that'll make it happy ;-p (I'm assuming it is actually assigned a value via reflection or something?)

Marc Gravell
You're right. It will be assiged by a DI framework, and that field should not be visible to the outside world.
Dylan Lin
The problem is, using a #pragma pair is really boring.
Dylan Lin
@Dylan - then give the code a value somewhere. Frankly huge chunks of most code might be considered "boring" - but they are critical.
Marc Gravell
Yeah, giving a value is a good solution. Thanks :-)
Dylan Lin
+2  A: 

Doesn't:

private string _field = null;

remove the warning as well?

Dean Harding
Yeah, a perfect solution. Many thanks!
Dylan Lin