ReSharper Help  

Extract Method

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 a method parameters or represent its return value.

The refactoring can be applied to any expression, not necessarily to the set of statements. In the table below, you can see some usage examples.

Before After
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;   
}
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;   
}
public void Foo(ICollection col)
{
   //check for empty collection
   if(col.GetEnumerator().MoveNext())
   {
      ...
   }
   ...
}
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 code block has multiple exit points (for example, the block has a conditional return statement), the Extract Method refactoring can change the code semantic and produce uncompilable 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.

To extract a method

  1. In the editor, select a block of code to be converted into a method.
  2. Do one of the following:
    • On the main menu, click ReSharper | Refactor | Extract Method.
    • Right-click the selection, and on the shortcut menu click Refactor | Extract Method.
    • Pres Ctrl + Alt + M.
    • Press Ctrl + Shift + R, and then select Extract Method.
  3. The Extract Method dialog box opens.

  4. In the Name text box, type the method name.
  5. If you want to create a static method, select the Declare static check box.
  6. In the Return value list, you can see the list of expressions that ReSharper detected as possible method return values (if any), or select No return value to create a void method.
  7. Note    Expressions detected as possible return values will have the out modifier in the parameters list.
  8. 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.
  9. In the Visibility area, specify the visibility of the new method.
  10. Check the results in the Signature preview text box, and click Continue.

If no conflicts are found, all the changes are applied immediately.

See Also

Refactoring Code