ReSharper Help

Extract Class

ReSharper | Refactor | Extract | Extract Class…
ReSharper_ExtractClass

This refactoring allows you to extract members of an existing class to a new class. For example, this refactoring can be helpful if 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 filed 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

Before refactoringAfter refactoring
abstract 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 Logger { public static void LogError(Exception e) { File.WriteAllText(@"c:\Errors\Exception.txt", e.ToString()); } } abstract class Shape { public void Draw() { try { // draw } catch (Exception e) { Logger.LogError(e); } } }

To extract a class

  1. Select a class in one of the following ways:
    • In the editor, set the caret at the name of a class.
    • Select a class in the Solution Explorer.
    • Select a class in the File Structure window.
    • Select a class in the Class View.
    • Select a class in the Object Browser.
    • Select a class in the type dependency diagram.
    Or, alternatively, select one or several members you want to extract in the editor or in a tool window.
  2. Do one of the following:
    • On the main menu, choose ReSharper | Refactor | Extract | Extract Class…
    • Right-click and choose Refactor | Extract Class on the context menu.
    • Press Ctrl+Shift+R and then choose Extract Class
    The Extract Class dialog will open.
  3. Specify a name for the 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 extracted into the class. ReSharper suggests to extract all members that use the selected members. 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. As soon as you select a member, it gets the 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 on the warning icon and choose one of the suggested fixes:
    Extract_Class_non_public
  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/technologies:

C# VB.NET C++ HTML ASPX Razor JavaScript TypeScript CSS XML XAML RESX Build Scripts
feature_available feature_available feature_available

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.

See Also

Last modified: 30 June 2015