ReSharper 2023.3 Help

Extract Class refactoring

This refactoring allows you to move members of an existing class to a new class. It will be helpful when you need to replace a single class that is responsible for multiple tasks with several classes each having a single responsibility.

ReSharper will automatically create a reference field that will provide access to the newly created class. If necessary, you can also leave the extracted members in the original class as copies or so that the implementation is delegated to the same members in the new class.

In the example below, we extract the LogError method into the new Logger class

class Shape { public void Draw() { try { /*draw*/ } catch (Exception e) { LogError(e); } } public static void LogError(Exception e) { File.WriteAllText(@"c:\Errors\Exception.txt", e.ToString()); } }
class Shape { public void Draw() { try { /*draw*/ } catch (Exception e) { Logger.LogError(e); } } } class Logger { public static void LogError(Exception e) { File.WriteAllText(@"c:\Errors\Exception.txt", e.ToString()); } }

Create a new class from selected members

  1. Select a class or a member that you want to move in one of the following ways:

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

    • Select a class or a member in the File Structure window.

    • Select a class or a member in the Class View.

    • Select a class or a member in the Object Browser.

    • Select a class or a member in the type dependency diagram.

  2. Do one of the following:

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

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

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

    The Extract Class dialog will open.

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

  4. Select members to be moved to the new class. When you select a member, ReSharper suggests extracting other members that have usages of the selected member. You can recognize the suggested members by the green Extract label.

    You do not have to accept this suggestion, but if you will, you can click Extract all suggested members to select all of them.

  5. All selected members have a selector in the Source class member column. If you want the member to be directly available in the current class, click this selector and choose Create delegating wrapper for methods and properties or Create copy for properties and fields.

  6. If you are extracting a non-public member, which is used by other members that you do not extract, ReSharper warns you that these usages will not work if the extracted member is left non-public. You can either select the dependent members by clicking Extract all suggested members or click the warning icon and choose one of the suggested fixes:

    ReSharper: Extract Class refactoring
  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. Extract 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: 18 March 2024