共用方式為


移除未讀取的私人成員 (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

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

另請參閱