Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The following procedure describes how to create a new method from a code fragment of an existing member. Use this procedure to perform the Extract Method refactoring operation.
To use Extract Method
Create a console application.
For more information, see Creating Console Applications (Visual C#).
In the Code Editor, select the code fragment you want to extract:
double area = PI * radius * radius.
On the Refactor menu, click Extract Method.
The Extract Method dialog box appears.
Alternatively, you can also type the keyboard shortcut CTRL+R, M to display the Extract Method dialog box.
You can also right-click the selected code, point to Refactor, and then click Extract Method to display the Extract Method dialog box.
Specify a name for the new method, such as CircleArea, in the New Method Name box.
A preview of the new method signature displays under Preview Method Signature.
Click OK.
Example
To set up this example, create a console application named ExtractMethod, and then replace Program with the following code. For more information, see Creating Console Applications (Visual C#).
class A
{
const double PI = 3.141592;
double CalculatePaintNeeded(double paintPerUnit, double radius)
{
// Select any of the following:
// 1. The entire next line of code.
// 2. The right-hand side of the next line of code.
// 3. Just "PI *" of the right-hand side of the next line
// of code (to see the prompt for selection expansion).
// 4. All code within the method body.
// ...Then invoke Extract Method.
double area = PI * radius * radius;
return area / paintPerUnit;
}
}