CA1821:必須移除空的完成項

型別名稱

RemoveEmptyFinalizers

CheckId

CA1821

分類

Microsoft.Performance

中斷變更

中斷

原因

型別實作空白的完成項,只呼叫基底型別完成項,或是只呼叫依條件發出的方法。

規則描述

請盡可能避免使用完成項,因為追蹤物件存留期 (Lifetime) 時將會產生額外的效能負荷。記憶體回收行程會在回收物件之前執行完成項。這表示它必須執行兩次回收作業才能回收物件。空白完成項只會增加這種額外負荷,而沒有任何好處。

如何修正違規

移除空白完成項。如果需要有完成項才能進行偵錯,請將整個完成項包含在 #if DEBUG / #endif 指示詞中。

隱藏警告的時機

請勿隱藏此規則的訊息。無法抑制最終化將會降低效能且不提供任何好處。

範例

下列範例顯示應該移除的空白完成項、應該包含在 #if DEBUG / #endif 指示詞中的完成項,以及正確使用 #if DEBUG / #endif 指示詞的完成項。

using System.Diagnostics;

public class Class1
{
    // Violation occurs because the finalizer is empty.
    ~Class1()
    {
    }
}

public class Class2
{
    // Violation occurs because Debug.Fail is a conditional method. 
    // The finalizer will contain code only if the DEBUG directive 
    // symbol is present at compile time. When the DEBUG 
    // directive is not present, the finalizer will still exist, but 
    // it will be empty.
    ~Class2()
    {
        Debug.Fail("Finalizer called!");
    }
}

public class Class3
{
    #if DEBUG
        // Violation will not occur because the finalizer will exist and 
        // contain code when the DEBUG directive is present. When the 
        // DEBUG directive is not present, the finalizer will not exist, 
        // and therefore not be empty.
        ~Class3()
        {
            Debug.Fail("Finalizer called!");
        }
    #endif
}