ReSharper 2018.1 Help

Inline Field refactoring

ReSharper | Refactor | Inline | Inline…
Ctrl+Alt+N
ReSharper_InlineVariable

This refactoring helps you replace a field with its value and remove its declaration. For obvious reasons, the refactoring can be applied to a field that has only one write usage. This may be a constant or readonly field, or a field that is initialized and used in the same function.
If the target field has conditional or complex assignment and it is used within a single function, ReSharper will create a local variable to handle the value of the field.

In the example below, we use this refactoring to inline a constant that has a single usage:

Before refactoringAfter refactoring
class Shape { private const string ErrorMessage = "Something has failed"; public void Draw(string s) { try { // draw } catch (Exception e) { Console.WriteLine("{0} : {1}", ErrorMessage, e); } } }
class Shape { public void Draw(string s) { try { // draw } catch (Exception e) { Console.WriteLine("{0} : {1}", "Something has failed", e); } } }

To inline a field

  1. Place the caret at the declaration or a usage of a field, which has only one write usage.
  2. Do one of the following:
    • Press Ctrl+Alt+N and then choose Inline Field
    • Press Ctrl+Shift+R and then choose Inline Field
    • Right-click and choose Refactor | Inline Field in the context menu.
    • Choose ReSharper | Refactor | Inline | Inline… in the main menu.
  3. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

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 Feature is available Feature is not available Feature is not available Feature is not available Feature is available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not available Feature is not 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.

Last modified: 20 August 2018

See Also