Single height = 5;
double volume = Math.PI * Math.Pow(radius, 2) * height;
If you exclude the variables declaration in the refactoring (instead of selecting all the three lines; see Figure 2-49) and name the new method VolumeofCylinder
, a method with two parameters is created:
private void Form1_Load(object sender, EventArgs e) {
Single radius = 3.5f;
Single height = 5;
}

Figure 2-49
Here are some observations:
□ Variables that are defined outside of the highlighted block for refactoring are used as an input parameter in the new method.
□ If variables are declared within the block selected for refactoring, the new method will have no signature.
□ Values that are changed within the block of highlighted code will be passed into the new method by reference.
You can use code refactoring to reorder the parameters in a function. Consider the following function from the previous example:
private static double VolumeofCylinder(Single radius, Single height) {
return Math.PI * Math.Pow(radius, 2) * height;
}
Highlight the function signature, right-click it, and select Refactor→Reorder Parameters (see Figure 2-50).

Figure 2-50
You can then rearrange the order of the parameter list (see Figure 2-51).

Figure 2-51
Click OK. You can preview the changes before they are made (see Figure 2-52).

Figure 2-52
Once you click the Apply button, your code is changed automatically:
private void Form1_Load(object sender, EventArgs e) {
Single radius = 3.5f;
Single height = 5;
}
return Math.PI * Math.Pow(radius, 2) * height;
}
All statements that call the modified function will have their arguments order changed automatically.
You can also remove parameters from a function by highlighting the function signature, right-clicking, and selecting Refactor→Remove Parameters. Then remove the parameter(s) you want to delete (see Figure 2- 53). All statements that call the modified function will have their calls changed automatically.

Figure 2-53
Consider the following string declaration:
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
private void Form1_Load(object sender, EventArgs e) {
//...
}
}
}
Instead of exposing the caption
variable as public, it is a better idea to encapsulate it as a property and use the set
and get
accessors to access it. To do that, right-click on the caption variable and select Refactor→Encapsulate Field (see Figure 2-54).

Figure 2-54
Assign a name to your property (see Figure 2-55). You have the option to update all external references or all references (including the one within the class), and you can choose to preview your reference changes. When you're ready, click OK.

Figure 2-55
After you've previewed the changes (see Figure 2-56), click Apply to effect the change.

Figure 2-56
Here is the result after applying the change:
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
private void Form1_Load(object sender, EventArgs e) {
//...
}
}
}
You can use the refactoring engine to extract an interface from a class definition. Consider the following Contact
class:
namespace WindowsFormsApplication1 {