Inline Parameter Refactoring in ReSharper 8

Recent ReSharper 8 EAP builds introduce a new refactoring for C# (VB.NET support is currently in the works) whereby we can refactor a parameter passed to a method to be replaced with actual value being passed in.

Before applying Inline Parameter refactoring

Invoking Inline Parameter refactoring on the argument 400 in the above screenshot causes the value parameter in the AddTen method to be removed, with all instances of its use replaced by the literal 400:

After applying Inline Parameter refactoring

The above is easy because the exact value to substitute is known. But that same refactoring can be invoked from the method parameter:

ReSharper 8 Inline Method Parameter

In this situation, the refactoring locates all the instances where the parameter is given a value. All you have to do is pick the instance that you want to use, and the parameter will be correspondingly inlined:

ReSharper 8 Inline Parameter Choose Usage Dialog

If the first usage from the above is chosen, the following changes will occur:

  • The original Solve method loses the last parameter, all its uses updated by the constant 16:

  • All methods that previously used this parameter no longer use it:
    ReSharper 8 inline parameter result

Finally, there are situations where a parameter that you wish to be inlined depends on local variables or other calculations the cannot be extracted. For example:

ReSharper 8 Inline Parameter Lambda Sample

Invoking Inline Parameter on the action parameter presents a dialog warning that while this is inlined, an additional parameter will be created:

ReSharper 8 Inline Parameter Choose Usage Replacement

Then, there is another dialog, which asks you if you want to include the new argument (in this case, work) in the method:

ReSharper 8 Inline Refactoring Inline Parameter Options Dialog

And, finally, the magic happens and the whole lambda gets transplanted into DoSomething(), with the usage updated appropriately:

ReSharper 8 Inline Parameter with Lambda End Result

This feature is already in the latest EAP, so if you want to see it today, you know what to do. Enjoy!

image description