ReSharper
 
2024.3
Get ReSharper

Generate Dispose Pattern

Last modified: 26 May 2024

ReSharper helps you automatically generate various implementations of IDisposable depending on kind of resources that your class owns:

  • If your class only owns managed IDisposable resources, ReSharper will help you generate a simple implementation of public void Dispose() without parameters, where Dispose() is called on selected disposable members.

  • If your class only owns unmanaged resources, ReSharper will additionally generate destructor and a ReleaseUnmanagedResources method where you could write your cleanup code.

  • If your class owns or will potentially own (via inheritance) both managed and unmanaged resources, ReSharper will additionally create a Dispose(bool disposing) method which could be overridden by the inheritors.

To configure how the disposable pattern is implemented, use option in the Generate dialog.

In the example below, this command is used to generate a disposable pattern for managed logger and resource fields, with a single option to generate null checks for these fields.

This action is also available as a quick-fix if IDisposable is not implemented.

A quick-fix that helps generate dispose pattern

This feature is supported in the following languages and technologies:

The instructions and examples given here address the use of the feature in C#. For more information about other languages, refer to corresponding topics in the Languages and frameworks section.