BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0

Implemented by classes that require explicit control over resource cleanup.

Objects that need to free resources that cannot safely be reclaimed by the garbage collector implement the interface.

It is a version breaking change to add the interface to an existing class, as it changes the semantics of the class.

contains the method. The consumer of an object should call this method when the object is no longer needed. The interface is generally provided for the release of unmanaged resources that need to be reclaimed in some order or time dependent manner. It is important to note that the actual release of these resources happens at the first call to for any given object that supports this interface. Programmers should take care to pair the creation of objects that implement with at most one invocation of the method. Though it is legal to invoke more than once, if this happens it might indicate the presence of a bug since such an object is usually rendered otherwise unusable after the first invocation.

- Method
  System.Void

  Performs application-defined tasks associated with freeing or resetting resources.

  This method is, by convention, used for all tasks associated with freeing resources held by an object, or preparing an object for reuse.

  When implementing the method, objects should seek to ensure that all held resources are freed by propagating the call through the containment hierarchy. For example, if an object A allocates an object B, and B allocates an object C, then A's implementation should call on B, which should in turn call on C. Objects should also call the method of their base class if the base class implements .

  If an object's method is called more than once, the object should ignore all calls after the first one. The object should not throw an exception if its method is called multiple times. can throw an exception if an error occurs because a resource has already been freed and had not been called previously.

  A resource type might use a particular convention to denote an allocated state versus a freed state. An example of this is stream classes, which are traditionally thought of as open or closed. Classes that have such conventions might choose to implement a public method with a customized name, which calls the method.

  Because the method must be called explicitly, objects that implement should also implement a finalizer to handle freeing resources when is not called. By default, the garbage collector will automatically call an object's finalizer prior to reclaiming its memory. However, once the method has been called, it is typically unnecessary and/or undesirable for the garbage collector to call the disposed object's finalizer. To prevent automatic finalization, implementations can call . For additional information on implementing finalizers, see and .

  Resource classes should follow the pattern illustrated by this example:

  `class ResourceWrapper : BaseType, IDisposable { // Pointer to a external resource. private int handle; private OtherResource otherRes; //Other resource you use. private bool disposed = false; public ResourceWrapper () { handle = //Allocate on the unmanaged side. otherRes = new OtherResource (...); } // Free your own state. private void freeState () { if (!disposed) { CloseHandle (handle); dispose = true; } } // Free your own state, call dispose on all state you hold, // and take yourself off the Finalization queue. public void Dispose () { freeState (); OtherRes.Dispose(); // If base type implements dispose, call it. base.Dispose(); GC.SuppressFinalize(this); } // Free your own state (not other state you hold) // and give your base class a chance to finalize. ~ResourceWrapper (){ freeState(); } }` 0 1.0.5000.0 2.0.0.0
0 System.Runtime.InteropServices.ComVisible(true)