ReSharper Help  

Introduce Parameter

The Introduce Parameter refactoring allows you to add a new parameter to a function from a local variable or a constant inside its body. All calls to the function are automatically updated with the appropriate value passed as the new parameter.

To introduce a parameter:

  1. In the code editor, select a local variable or a constant inside a function.
  2. Do one of the following:
    • Select ReSharper | Refactor | Introduce Parameter in the main menu.
    • Right-click on the name of the field in the editor and select Refactor | Introduce Parameter in the context menu. 
    • Press Ctrl + Shift + R and select Introduce Parameter.
  3. In the dialog that appears, select the necessary options and click Continue.

Example

  1. In the function below, we would like to pass the day of the week as a parameter. We select the constant that determines the pay day:

  2. With the constant selected, invoke the Introduce Parameter refactoring. The following dialog is displayed:

  3. Do the following to apply the refactoring successfully:
    • Select the type of the new parameter. ReSharper suggests the type based on the type of the variable/constant.
    • Type the new parameter name. ReSharper suggests a name based on the name of the variable/constant.
    • Select to Replace all occurrences of the constant if you want to insert the value of the selected variable/constant in all calls to the function. If only occurrence is present in the existing code, the checkbox will be unavailable.

     

  4. After you click Continue, the code is updated as shown below:

If no conflicts are found, the changes are applied immediately.

See Also

Refactoring Code