ReSharper 2024.2 Help

Extract Superclass refactoring

This refactoring allows you to extract certain members from a selected class into a new base class. The original class will extend the created base class.

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

If you want to create an abstract class, mark at least one of the extracted method abstract.

In the example below, we extract an abstract class Shape from the existing class Circle:

public class Circle : IDrawable { public Point Center { get; private set; } public Color MyColor { get; private set; } public int Radius { get; private set; } public void Draw() { // draw... } } public interface IDrawable { void Draw(); }
public abstract class Shape : IDrawable { public Color MyColor { get; private set; } public abstract void Draw(); } public class Circle : Shape { public Point Center { get; private set; } public int Radius { get; private set; } public override void Draw() { // draw... } } public interface IDrawable { void Draw(); }

Extract a base class

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

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

    • Select a class in the Solution Explorer.

    • Select a class in the File Structure window window.

    • Select a class in the Class View.

    • Select a class in the Object Browser.

    • Select a class in the type dependency diagram.

  2. Do one of the following:

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

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

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

    The Extract Superclass dialog will open.

  3. Specify a name for the new base class 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 base class. The list of members can contain:

    • Members of the current type

    • Interfaces that the selected type currently implements

  5. To make/keep individual members abstract, tick the checkbox in the Make Abstract column next to them. If there is at least one member marked abstract, the base class will be created abstract too.

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

  7. If you select members that reference other members, there will be undefined symbols in the extracted class. Click Dependent to resolve the conflict by selecting all members that depend on the currently selected members.

  8. To apply the refactoring, click Next.

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

ReSharper. Extract Superclass (Base class) refactoring

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