ReSharper 2024.2 Help

Extract Interface refactoring

This refactoring helps create a new interface based on a selected type. ReSharper suggests choosing members to be transferred to the new interface. After extraction, the original type is updated to implement the new interface.

If the current type already implements any interfaces, those interfaces can also be extracted into the new interface.

In the example below, the Color property and the Draw method of the Circle class are extracted to create a new interface:

class Circle { public Point Center { get; private set; } public Color Color { get; private set; } public int Radius { get; private set; } public void Draw() { // draw... } }
interface IShape { Color Color { get; } void Draw(); } class Circle : IShape { public Point Center { get; private set; } public Color Color { get; private set; } public int Radius { get; private set; } public void Draw() { // draw... } }

Extract an interface from a type

  1. Select a type in one of the following ways:

    • In the editor, place the caret at the name of a type.

    • Select a type in the Solution Explorer.

    • Select a type in the File Structure window window.

    • Select a type in the Class View.

    • Select a type in the Object Browser.

    • Select a type in the type dependency diagram.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Extract Interface.

    • Right-click and choose Refactor | Extract Interface from the context menu.

    • Choose ReSharper | Refactor | Extract | Extract Interface… from the main menu.

    The Extract Interface dialog will open.

  3. Specify a name for the new interface and where it should be placed — in a new file or in the same file as the original type.

  4. Select members that you want to transfer to the new interface. The list of members can contain:

    • Members of the current type

    • Members of base types that the selected type currently inherits/implements

    • Interfaces that the selected type currently implements

  5. To quickly select all public members, click All Public.

  6. If you have selected members that reference other members, the latter are highlighted with red. Click Dependent to resolve the conflict, that is to select any members that depend on the currently selected members.

  7. To apply the refactoring, click Next.

  8. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

ReSharper. Extracting interface from a class

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic

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 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 more information about other languages, refer to corresponding topics in the ReSharper by language section.

Last modified: 23 September 2024