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

前端 未结 5 2072
春和景丽
春和景丽 2020-12-18 17:54

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

5条回答
  •  太阳男子
    2020-12-18 18:23

    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.

提交回复
热议问题