Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable/suppress warning CS0649 in C# for a specific field of class

I have some fields in a C# class which I initialize using reflection. The compiler shows CS0649 warning for them:

Field foo' is never assigned to, and will always have its default valuenull' (CS0649) (Assembly-CSharp)

I'd like to disable the warning for these specific fields only and still let the warning be shown for other classes and other fields of this class. It is possible to disable CS0649 for the whole project, is there anything more fine-grained?

like image 639
iseeall Avatar asked Dec 05 '12 15:12

iseeall


3 Answers

You could use #pragma warning to disable and then re-enable particular warnings:

public class MyClass {     #pragma warning disable 0649      // field declarations for which to disable warning     private object foo;      #pragma warning restore 0649      // rest of class } 

Refer to Suppressing “is never used” and “is never assigned to” warnings in C# for an expanded answer.

like image 120
Douglas Avatar answered Sep 24 '22 02:09

Douglas


I believe it's worth noting the warning can also be suppressed by using inline initialization. This clutters your code much less.

public class MyClass {     // field declarations for which to disable warning     private object foo = null;      // rest of class } 
like image 36
BMac Avatar answered Sep 23 '22 02:09

BMac


//disable warning here
#pragma warning disable 0649

 //foo field declaration

//restore warning to previous state after
#pragma warning restore 0649
like image 38
Alexander Bortnik Avatar answered Sep 22 '22 02:09

Alexander Bortnik