We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

CA1821: Remove empty finalizers







Breaking Change


A type implements a finalizer that is empty, calls only the base type finalizer, or calls only conditionally emitted methods.

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.

Remove the empty finalizer. If a finalizer is required for debugging, enclose the whole finalizer in #if DEBUG / #endif directives.

Do not suppress a message from this rule. Failure to suppress finalization decreases performance and provides no benefits.

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.

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.
		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.
			Debug.Fail("Finalizer called!");