Inline Field refactoring
Refactor | Inline...
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, JetBrains Rider 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 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); } }}
After refactoring
class Shape{ public void Draw(string s) { try { /*draw*/ } catch (Exception e) { Console.WriteLine("{0} : {1}", "Something has failed", e); } }}
tip
The reverse functionality is available with the Introduce Field refactoring refactoring.
Place the caret at the declaration or a usage of a field, which has only one write usage.
Do one of the following:
Press and then choose Inline Field
Press CtrlShift0R and then choose Inline Field.
Choose Refactor | Inline Field from the main menu.
If no conflicts are found, JetBrains Rider performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.
tip
Right after the refactoring has been completed, all changes that it makes anywhere, including other files, are registered as a single operation. So you can use the Undo action Ctrl0Z to roll back all these changes with a single keystroke.