CA1821:移除空的终结器

类型名

RemoveEmptyFinalizers

CheckId

CA1821

类别

Microsoft.Performance

是否重大更改

非重大更改

原因

类型实现空终结器、只调用基类型终结器,或者只调用按条件发出的方法。

规则说明

应尽可能避免终结器,因为跟踪对象生存期会产生额外的性能系统开销。 垃圾收集器将在收集该对象之前运行终结器。 这意味着收集该对象需要两个集合。 空的终结器只会徒增这种系统开销,而没有一点好处。

如何解决冲突

移除空的终结器。 如果进行调试时需要一个终结器,请将整个终结器括在 #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
}