TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
C# Coding Standard

Rule:  5@112Checked automatically with code checker

Synopsis:Provide a method that will cause the finalizer not to be called
Severity Level:3
Category:Object lifecycle


If a finalizer is needed to verify that a user has called certain cleanup methods, call GC.SuppressFinalize in the Close() method. This ensures that the finalizer is ignored if the user is properly using the class. The following snippet illustrates this pattern.

public class IpcPeer
	bool connected = false;

	public void Connect()
		// Do some work and then change the state of this object.
		connected = true;

	public void Close()
		// Close the connection, change the state, and instruct garbage collector
		// not to call the finalizer.
		connected = false;

		// If the finalizer is called, then Close() was not called.
		if (connected)
			// Warning! User has not called Close(). Notice that you can't 
			// call Close() from here because the objects involved may 
			// have already been garbage collected (see Rule 5@113).