ReSharper 2024.3 Help

Replace Constructor with Factory Method refactoring

This refactoring helps implement the factory method pattern for an existing class. If you opt to create the factory method in the same class, the refactoring makes the selected constructor private and encapsulates it into a static method that returns a new instance of the class. You can also choose to create the factory method in any other class. In this case, the constructor stays public, you can change its access modifier later, if necessary.

If there are any usages of the constructor, they are replaced with factory method calls.

In the example below, we use the refactoring to add the factory method in the same class:

class Foo { public Foo() { // instance initialization } }
class Foo { public static Foo CreateFoo() { return new Foo(); } private Foo() { // instance initialization } }

Replace a constructor with a factory method

  1. In the editor, place the caret at a constructor or choose a constructor in the File Structure window window.

  2. Do one of the following:

    • Press Control+Shift+R and then choose Replace Constructor with Factory Method.

    • Right-click and choose Refactor | Replace Constructor with Factory Method from the context menu.

    • Choose ReSharper | Refactor | Convert | Constructor to Factory Method… from the main menu.

    The Replace Constructor with Factory Method dialog will open.

  3. Specify a name for the factory method or accept the suggested name.

  4. By default, the refactoring creates factory method in the same class. If you want to create the factory method in another class, specify the fully qualified name of the class.

  5. To apply the refactoring, click Next.

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

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

Last modified: 30 July 2024