iseeall iseeall - 1 year ago 284
C# Question

Disable/supress 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:

foo' is never assigned to, and will always have its default
null' (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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download