The Introduce Parameter refactoring allows you to add a new parameter into a method or constructor from an expression within its body. All calls to the method/constructor are automatically updated with the appropriate value passed as the new parameter.
If an expression that you want to pass as a parameter references symbols declared in the method body, ReSharper allows to "en-lambda" these symbols by introducing a delegate parameter and using it to pass a lambda expression from the caller (see procedure below for an example).
- In the editor, select an expression within a method or constructor.
Do one of the following:
- On the main menu, choose ReSharper| Refactor | Introduce Parameter.
- Choose Refactor | Introduce Parameter on the context menu.
- Press Ctrl+Shift+RCtrl+Shift+R, then click Introduce Parameter.
- Press Ctrl+Alt+PCtrl+R,P.
If the selected expression occurs two or more times in the current method or constructor,
ReSharper asks to specify how many occurrences should be replaced.
If the selected expression occurs only once,
the Introduce Parameter dialog box opens immediately.
- Enter the name for a new parameter in the Parameter name text box.
- If the selected expression requires passing the parameter as a lambda expression, select members to en-lambda in the Select local variables to en-lambda section.
- Click Next. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, resolve conflicts.
Consider the example below:
After applying the refactoring, the expression is replaced with the getAttribute parameter.