CA1821: Remove empty finalizers
Visual Studio 2010
TypeName | RemoveEmptyFinalizers |
CheckId | CA1821 |
Category | Microsoft.Performance |
Breaking Change | Non-breaking |
Whenever you can, avoid finalizers because of the additional performance overhead that is involved in tracking object lifetime. The garbage collector will run the finalizer before it collects the object. This means that two collections will be required to collect the object. An empty finalizer incurs this added overhead without any benefit.
The following example shows an empty finalizer that should be removed, a finalizer that should be enclosed in #if DEBUG / #endif directives, and a finalizer that uses the #if DEBUG / #endif directives correctly.
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 }