移除未讀取的私人成員 (IDE0052)

屬性
規則識別碼 IDE0052
標題 移除未讀取的私人成員
類別 CodeQuality
子類別 運算式層級喜好設定 (不必要的程式碼規則)
適用語言 C# 和 Visual Basic

概觀

此規則會標幟有一或多個寫入參考,但沒有讀取參考的私人欄位和屬性。 在此情節中,可以重構或移除程式碼的某些部分,修正可維護性、效能或功能問題。

選項

此規則沒有相關聯的程式碼樣式選項。

範例

// Code with violations
class C
{
    // IDE0052: Remove unread private members
    private readonly int _field1;
    private int _field2;
    private int Property { get; set; }

    public C()
    {
        _field1 = 0;
    }

    public void SetMethod()
    {
        _field2 = 0;
        Property = 0;
    }
}

// Fixed code
class C
{
    public C()
    {
    }

    public void SetMethod()
    {
    }
}

隱藏警告

若您只想隱藏單一違規,請將前置處理指示詞新增至來源檔案以停用規則,然後重新啟用規則。

#pragma warning disable IDE0052
// The code that's violating the rule is on this line.
#pragma warning restore IDE0052

若要停用檔案、資料夾或專案的規則,請在組態檔中將其嚴重性設定為 none

[*.{cs,vb}]
dotnet_diagnostic.IDE0052.severity = none

若要停用整個類別的規則,請在組態檔中將類別的嚴重性設定為 none

[*.{cs,vb}]
dotnet_analyzer_diagnostic.category-CodeQuality.severity = none

如需詳細資訊,請參閱如何隱藏程式碼分析警告

另請參閱