.NET Tools
Essential productivity kit for .NET and game developers
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.
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
:
The above is easy because the exact value to substitute is known. But that same refactoring can be invoked from the 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:
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 constant16
:
-
All methods that previously used this parameter no longer use it:
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:
Invoking Inline Parameter on the action
parameter presents a dialog warning that while this is inlined, an additional parameter will be created:
Then, there is another dialog, which asks you if you want to include the new argument (in this case, work
) in the method:
And, finally, the magic happens and the whole lambda gets transplanted into DoSomething()
, with the usage updated appropriately:
This feature is already in the latest EAP, so if you want to see it today, you know what to do. Enjoy!