Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Extract Method 

Extract Method is a Refactoring operation that provides an easy way to create a new method from a code fragment in an existing member.

Using Extract Method, you can create a new method by extracting a selection of code from inside the code block of an existing member. The new method is created containing the selected code, and the selected code in the existing member is replaced with a call to the new method. Turning a fragment of code into its own method gives you the ability to quickly and accurately reorganize code for better reuse and readability.

Extract Method has the following benefits:

  • Encourages best coding practices by emphasizing discrete, reusable methods.

  • Encourages self-documenting code through good organization. When descriptive names are used, high-level methods can read more like a series of comments.

  • Encourages the creation of finer-grained methods to simplify overriding.

  • Reduces code duplication.

When you use the Extract Method command, the new method is inserted following the source member in the same class.

Partial Types

If the class is a partial type, then Extract Method generates the new method immediately following the source member. Extract Method determines the signature of the new method, creating a static method when no instance data is referenced by the code within the new method.

Generic Type Parameters

When you extract a method that has an unconstrained generic type parameter, the generated code will not add the "ref" modifier to that parameter unless a value is assigned to it. If the extracted method will support reference types as the generic type argument, then you should manually add the "ref" modifier to the parameter in method signature.

Anonymous Methods

If you attempt to extract part of an anonymous method that includes a reference to a local variable that is either declared or referenced outside of the anonymous method, then Visual Studio will warn you about potential semantic changes. Specifically, the moments of when the value of the local variable is passed to the anonymous method will differ.

When an anonymous method uses the value of a local variable, the value is obtained at the moment the anonymous method is executed. When an anonymous method is extracted into another method, the value of the local variable is obtained at the moment of the call to the extracted method.

The following example illustrates this semantic change. If this code is executed, then 11 will be printed to the console. If you use Extract Method to extract the region of code that is marked by code comments into its own method and then execute the refactored code, then 10 will be printed to the console.

class Program
{
    delegate void D();
    D d;
    static void Main(string[] args)
    {
        Program p = new Program();
        int i = 10;
        /*begin extraction*/
            p.d = delegate { Console.WriteLine(i++); };
        /*end extraction*/
        i++;
        p.d();
    }
}

To work around this situation, make the local variables that are used in the anonymous method, fields of the class.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft