Introduce Parameter refactoring
Refactor | Extract/Introduce | Introduce Parameter...
Ctrl0R,0P
This refactoring allows you to move an expression from a method implementation to its callers by adding a new parameter. All occurrences of the expression are replaced with the new parameter; all calls to the method in the solution are updated with the new argument.
In the example below, we use this refactoring to replace two occurrences of the same string with a parameter. The string itself is moved to the caller argument:
Before refactoring
abstract class Shape{ public void Draw() { try { /*draw*/ } catch (Exception e) { LogError(e); } } static void LogError(Exception ex) { Console.WriteLine("Something has failed..."); File.WriteAllText(@"c:\Error.txt", "Something has failed..." + ex); }}
After refactoring
abstract class Shape{ public void Draw() { try { /*draw*/ } catch (Exception e) { LogError(e, "Something has failed..."); } } static void LogError(Exception ex, string message) { Console.WriteLine(message); File.WriteAllText(@"c:\Error.txt", message + ex); }}
If the expression that you want to pass as a parameter references variables declared in the method body, JetBrains Rider allows you to 'enlambda' these variables by introducing a generic delegate parameter and using it to pass a lambda expression from the caller. In the example below, we invoke the refactoring for the "The current time is: " + currentTme
expression:
Before refactoring
static void PrintCurrentTime(){ var currentTme = DateTime.Now.ToString("h:mm:ss tt"); Console.WriteLine("The current time is: " + currentTme);}private void Test1(){ PrintCurrentTime();}
After refactoring
static void PrintCurrentTime(Func<string, string> output){ var currentTme = DateTime.Now.ToString("h:mm:ss tt"); Console.WriteLine(output(currentTme));}private void Test1(){ PrintCurrentTime(currentTme => "The current time is: " + currentTme);}
In the editor, select an expression within a method or constructor.
Do one of the following:
Press Ctrl0R,0P.
Press CtrlShift0R and then choose Introduce Parameter.
Choose Refactor | Introduce Parameter from the main menu.
If more than one occurrence of the selected expression is found, JetBrains Rider displays the drop-down menu where you can choose whether to apply the refactoring to all occurrences or only to the current one.
In the Introduce Parameter dialog that appears, enter the name for a new parameter.
If the selected expression is a constant or is of a value type, then you can use the expression as a default value for an introduced parameter. To do so, select the Make default checkbox.
If the selected expression depends on other variables, these variables appear in the Select variables to enlambda section. Select the variables that you want to use in the generic delegate (which will be added as the parameter in this case).
To apply the refactoring, click Next.
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.
![JetBrains Rider: 'Introduce Parameter' refactoring JetBrains Rider: 'Introduce Parameter' refactoring](https://resources.jetbrains.com/help/img/rider/2024.3/Refactorings__Introduce_Parameter__dialog_box.png)