ReSharper Web Help

The Extract Method refactoring allows users to quickly create a new method based on a selected code fragment. ReSharper analyses the selected block of code and detects variables that can be converted into method parameters or represent its return value. This refactoring can be applied to any expression.

The reverse functionality is provided by the Inline Method refactoring.

To extract a method
  1. In the editor, select a block of code to be converted into a method.
  2. On the ReSharper menu or context menu, choose Refactor | Extract Method. The Extract Method dialog box opens:
  3. In the Name text box, type the method name.
  4. If you want to create a static method, select Declare static.
  5. From the Return value list, select one of expressions that ReSharper detected as possible method return values (if any). To create a void method, select No return value.

     Note

    Expressions detected as possible return values will have the out modifier in the parameters list.
  6. In the Parameters area, you can do the following:
    • Change the names of the detected method parameters.
    • Reorder the parameters using the Move Up and Move Down buttons.
    • Include or exclude parameters by selecting or clearing the corresponding check boxes. If you exclude a parameter from the list, the local variable with the same name and type will be created in the method. The variable will be initialized by the "..." value that you need to correct manually.
  7. In the Visibility area, specify the visibility of the new method.
  8. Check the results in the Signature preview text box, and click Continue.

The tables below contain two comprehensive examples of how the Extract Method refactoring is applied.

Using Extract Method (1)
Before:
public ArrayList Foo()
{
    string[] strings = { "a", "ab", "abc" };
    char[] chars = { 'a', 'b', 'c' };
    ArrayList list = new ArrayList(strings);
    for (int i = 0; i<list.Count; i++)
        {
            list[i] = ((string)list[i]).TrimEnd(chars);
        }

    return list;
}
After:
public void Foo()
{
    string[] strings = { "a", "ab", "abc" };
    char[] chars = { 'a', 'b', 'c' };
    ArrayList list = TrimStrings(chars, strings);
    Console.WriteLine(list);

    return list;
}

// extracted method
private ArrayList TrimList(char[] chars, string[] strings)
{
    ArrayList list = new ArrayList(strings);
    for (int i = 0; i<list.Count; i++)
    {
        list[i] = ((string)list[i]).TrimEnd(chars);
    }

    return list;
}
Using Extract Method (2)
Before:
public void Foo(ICollection col)
{
//  check for empty collection
    if(col.GetEnumerator().MoveNext())
    {
        ...
    }
    ...
}
After:
public void Foo(ICollection col)
{
//  check for empty collection
    if(IsEmpty(col))
    {
        ...
    }
    ...
}

//  extracted method
public bool IsEmpty(ICollection col)
{
    return col.GetEnumerator().MoveNext();
}

 Warning

If the selected block of code has multiple exit points (for example, the block has a conditional return statement), the Extract Method refactoring might change the code semantics and produce non-compilable code.

Consider the following example:

public void Foo()
{
    ...
    if(i>0)
        return;
    i++;
    Console.WriteLine(i);
   ...
}

If you try to extract a method from the selected code block, ReSharper will show a warning notifying that the refactoring is not safe.

See Also

Procedures