JetBrains Rider
 
Get JetBrains Rider
Get your hands on the new features ahead of the release by joining the Early Access Program for Rider 2025.1! Learn more

Generate Dispose Pattern

Last modified: 26 May 2024

JetBrains Rider 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, JetBrains Rider 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, JetBrains Rider 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, JetBrains Rider 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