ReSharper 2021.3 Help

Introduce Parameter refactoring

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:

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); } }
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 symbols declared in the method body, ReSharper allows you to 'en-lambda' these symbols 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:

static void PrintCurrentTime() { var currentTme = DateTime.Now.ToString("h:mm:ss tt"); Console.WriteLine("The current time is: " + currentTme); } private void Test1() { PrintCurrentTime(); }
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); }

Introduce a parameter for the selected expression

  1. In the editor, select an expression within a method or constructor.

  2. Do one of the following:

    • Press Control+Alt+P.

    • Press Control+Shift+R and then choose Introduce Parameter

    • Right-click and choose Refactor | Introduce Parameter in the context menu.

    • Choose ReSharper | Refactor | Introduce Parameter… in the main menu.

  3. If more than one occurrence of the selected expression is found, ReSharper displays the drop-down menu where you can choose whether to apply the refactoring to all occurrences or only to the current one.

  4. In the Introduce Parameter dialog that appears, enter the name for a new parameter.

  5. 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.

  6. If the selected expression depends on other variables, these variables appear in the Select local variables to en-lambda section. Select the variables that you want to use in the generic delegate (which will be added as the parameter in this case).

  7. To apply the refactoring, click Next.

  8. If no conflicts are found, ReSharper performs the refactoring immediately. Otherwise, it prompts you to resolve conflicts.

ReSharper: 'Introduce Parameter' refactoring

This feature is supported in the following languages and technologies:

Language: C#

Language: VB.NET

Language: C++

Language: HTML

Language: ASP.NET

Language: Razor

Language: JavaScript

Language: TypeScript

Language: CSS

Language: XML

Language: XAML

Language: Resx

Language: Build Scripts

Language: Protobuf

Language: JSON

Feature is available in C#

Feature is available in Visual Basic .NET

Feature is not available in C++

Feature is not available in HTML

Feature is not available in ASP.NET

Feature is not available in Razor

Feature is not available in JavaScript

Feature is not available in TypeScript

Feature is not available in CSS

Feature is not available in XML

Feature is not available in XAML

Feature is not available in Resource files

Feature is not available in build script files

Feature is not available in Protobuf

Feature is not available in JSON

The instructions and examples given here address the use of the feature in C#. For details specific to other languages, see corresponding topics in the ReSharper by language section.

Last modified: 07 April 2022