共用方式為


移除未使用的私人成員 (IDE0051)

財產 價值
規則標識碼 IDE0051
標題 移除未使用的私有成員
類別 CodeQuality
子類別 不必要的程式碼規則(表達式層級偏好)
適用的語言 C# 和 Visual Basic

概述

此規則會標示所有沒有讀取或寫入參考的未使用私有方法、欄位、屬性和事件。

選項

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

// Code with violations
class C
{
    // IDE0051: Remove unused private members
    private readonly int _fieldPrivate;
    private int PropertyPrivate => 1;
    private int GetNumPrivate() => 1;

    // No IDE0051
    internal readonly int FieldInternal;
    private readonly int _fieldPrivateUsed;
    public int PropertyPublic => _fieldPrivateUsed;
    private int GetNumPrivateUsed() => 1;
    internal int GetNumInternal() => GetNumPrivateUsed();
    public int GetNumPublic() => GetNumPrivateUsed();
}

// Fixed code
class C
{
    // No IDE0051
    internal readonly int FieldInternal;
    private readonly int _fieldPrivateUsed;
    public int PropertyPublic => _fieldPrivateUsed;
    private int GetNumPrivateUsed() => 1;
    internal int GetNumInternal() => GetNumPrivateUsed();
    public int GetNumPublic() => GetNumPrivateUsed();
}

隱藏警告

如果您想要只隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。

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

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

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

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

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

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

另請參閱