coderushforroslyn-117331-refactoring-assistance-decompose-parameter.md
This Refactoring is used to improve encapsulation. It changes the method signature, allowing you to pass only the part or parts of the object instead of the object itself. This Refactoring often helps in promoting reuse by reducing the complexity of the objects required to call a method.
Note
Available when the caret is on a method parameter or argument, assuming it is a class, interface or structure.
Place the caret on an appropriate item in a method parameters or arguments list.
Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
Select Decompose Parameter from the menu.
After execution, the Refactoring detects which fields and properties were used by the method and changes the method signature so that it accepts only the required fields. Then it changes the method calls according to the new signature.
static bool IsAdult(int age) => age >= 18;
static void Main(string[] args) {
var andrew = new Person("Andrew Fuller", 34);
if (!IsAdult(andrew.Age)) return;
Console.WriteLine($"{andrew.FullName} is adult");
Console.ReadKey();
}
Function IsAdult(age As Integer) As Boolean
Return age >= 18
End Function
Sub Main()
Dim andrew As New Person("Andrew Fuller", 34)
If Not IsAdult(andrew.Age) Then
Return
End If
Console.WriteLine($"{andrew.FullName} is adult")
Console.ReadKey()
End Sub
See Also
Decompose Parameter