Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 1.01 KB

File metadata and controls

41 lines (32 loc) · 1.01 KB

Pattern: Use of empty finalizer

Issue: -

Description

Whenever you can, avoid finalizers because of the additional performance overhead that's involved in tracking object lifetime. The garbage collector runs the finalizer before it collects the object. This means that at least two collections are required to collect the object. An empty finalizer incurs this added overhead without any benefit.

Example of incorrect code:

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

Example of correct code:

public class TestClass
{
#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.
	~TestClass()
	{
		Debug.Fail("Finalizer called!");
	}
#endif
}

Further Reading