ReSharper 2021.1 Help

Generate Dispose Pattern

ReSharper | Edit | Generate Code | Dispose Pattern
Alt+Insert | Dispose Pattern
ReSharper_GenerateDisposePattern

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.

Before generationAfter generation
class MyClass { private Logger logger; private IDisposable resource; }
class MyClass : IDisposable { private Logger logger; private IDisposable resource; public void Dispose() { logger?.Dispose(); resource?.Dispose(); } }

Generate a dispose pattern

  1. In the editor, set the caret at the type name or within a type at the line where you want to insert a dispose pattern. If the caret is on the type name, the generated code will be added in the beginning of the type declaration.

  2. Press Alt+Insert or choose ReSharper | Edit | Generate Code… from the main menu. Alternatively, you can press Control+Shift+A, start typing the command name in the popup, and then choose it there.

  3. In the Generate popup, select Dispose Pattern.

  4. In the Generate dialog that appears, you will see a list of private properties and fields of type assignable to IDisposable. Select some or all of these type members, and ReSharper will generate wrappers in the current type that delegate execution to selected type members.
    Generating a dispose pattern with ReSharper

    Optionally, use the following controls in the dialog:

    • Fields can be null — appears if there are any nullable fields or properties in your type. By default, this checkbox is selected, meaning that ReSharper will generate null checks for selected fields. You can clear this checkbox if you do not need null checks.

    • I have unmanaged resources — if this checkbox is selected, ReSharper will additionally generate destructor and a ReleaseUnmanagedResources method where you could write your cleanup code.

    • I plan to inherit from this class — if this checkbox is selected, ReSharper will additionally create a Dispose(bool disposing) method which could be overridden by the inheritors.

    • Dispose already exists — appears if an implementation of Dispose() already exists and lets you choose whether to:
      • Replace the method if it already exists.

      • Put the newly generated method side by side with the existing one.

      • Skip generating a new method altogether.

    • Destructor already exists — appears if you have chosen the I have unmanaged resources option and the class already has a destructor. Use this selector to choose whether to:
      • Replace the method if it already exists.

      • Put the newly generated method side by side with the existing one.

      • Skip generating a new method altogether.

  5. Click Finish to complete the wizard.
    You can also click Options to review or modify common code generation preferences on the Code Editing | Members Generation page of ReSharper options.

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:

Language: C#Language: VB.NETLanguage: C++Language: HTMLLanguage: ASP.NETLanguage: RazorLanguage: JavaScriptLanguage: TypeScriptLanguage: CSSLanguage: XMLLanguage: XAMLLanguage: ResxLanguage: Build ScriptsLanguage: ProtobufLanguage: JSON
Feature is available in C#Feature is available in Visual Basic .NET Feature is not available in C++ Feature is not available in HTML Feature is not available in ASP.NET Feature is not available in Razor Feature is not available in JavaScript Feature is not available in TypeScript Feature is not available in CSS Feature is not available in XML Feature is not available in XAML Feature is not available in Resource files Feature is not available in build script files Feature is not available in Protobuf Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by Language section.

Last modified: 08 March 2021