ReSharper 2024.3 Help

Introduce Field refactoring

This refactoring allows you to create a new field based on a selected expression, initialize it with the expression or from the constructor, and replace occurrences of the expression in the current type with references to the newly introduced field.

In the example below, we use this refactoring to replace two occurrences of the same string with a new constant field:

class ErrorHandler { public static void LogError(Exception e) { File.WriteAllText(@"c:\Error.txt", "Something has failed" + e); } public static void PrintError(Exception e) { Console.WriteLine("{0} : {1}", "Something has failed", e); } }
class ErrorHandler { private const string ErrorMessage = "Something has failed"; public static void LogError(Exception e) { File.WriteAllText(@"c:\Error.txt", ErrorMessage + e); } public static void PrintError(Exception e) { Console.WriteLine("{0} : {1}", ErrorMessage, e); } }

Introduce a field

  1. Select an expression in the editor.

  2. Do one of the following:

    • Press Control+Alt+D.

    • Press Control+Shift+R and then choose Introduce Field.

    • Right-click and choose Refactor | Introduce Field from the context menu.

    • Choose ReSharper | Refactor | Introduce Field… from the main menu.

  3. The Introduce Field dialog opens. Specify the name for the new field and choose the access modifier. Optionally, specify whether to add static and readonly modifiers to the field.

  4. Choose how to initialize the field:

    • Current member: initializes the field in the current member (this option is only available if you chose to replace a single occurrence or if all occurrences are within the current member).

    • Field initializer: initializes the field in the declaration.

    • Constructor(s): initializes the field in the constructor or constructors of the containing class; if there are no constructors, a parameterless constructor is created to initialize the field.

    • Introduce constant: creates a constant field. This option is only available if the value of the selected expression corresponds to a built-in type.

  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: Introduce Field 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

Introduce field 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 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 Languages and frameworks section.

Last modified: 23 September 2024