Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress a compiler warning in C# without using #pragma?

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!

like image 851
Mouhong Lin Avatar asked Apr 16 '10 04:04

Mouhong Lin


2 Answers

Doesn't:

private string _field = null;

remove the warning as well?

like image 83
Dean Harding Avatar answered Nov 02 '22 08:11

Dean Harding


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?)

like image 39
Marc Gravell Avatar answered Nov 02 '22 10:11

Marc Gravell