The Introduce Parameter refactoring allows you to add a new parameter into a method or constructor from an expression inside 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 code editor, select an expression inside a method or constructor:
- Choose ReSharper | Refactor | Introduce Parameter, or press Ctrl+Alt+P.
- If the selected expression occurs two or more times in the current method or
constructor, ReSharper asks to specify how many
occurrences it should replace:
- If the selected expression occurs only once, the Introduce Parameter
wizard opens immediately:
- If the selected expression occurs two or more times in the current method or constructor, ReSharper asks to specify how many occurrences it should replace:
- Specify the following options:
- In the Parameter name text box, choose a suggested name for the new parameter, or enter a custom name.
- 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 ReSharper discovers no conflicts, the refactoring
is applied immediately. The following figure shows how ReSharper generates
a delegate parameter:
This is how a method call transforms to pass a lambda expression: