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

纵然是瞬间 提交于 2019-12-01 02:39:32
Douglas

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.

//disable warning here
#pragma warning disable 0649

 //foo field declaration

//restore warning to previous state after
#pragma warning restore 0649

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

public class MyClass
{
    // field declarations for which to disable warning
    private object foo = null;

    // rest of class
}
public class YouClass
{
#pragma warning disable 649
    string foo;
#pragma warning restore 649
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!