Skip to main content

Inline Method and Delete

  • 2 minutes to read

Purpose

Replaces the current method call(s) with the method’s body and deletes the method. Use this refactoring when the method has a simple structure or is called only once.

Availability

This refactoring is available when the caret is in a method’s name inside the method call or declaration. This refactoring affects all method calls If you run it from the method declaration.

Usage

  1. Place the caret in a method name.

    Note

    The blinking cursor shows the caret’s position at which the Refactoring is available.

    public static double ConvertToUSD(Currency source, double amount) {
        return amount / GetCurrencyRate(source);
    }
    static void Main(string[] args) {
        double price = ConvertToUSD(Currency.JPY, 888);
        // ...
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.

  3. Select Inline Method and Delete from the menu.

    inline-method-and-delete-menu

After execution, this refactoring replaces the method call with this method body content and deletes the method. Each parameter reference in the method’s scope is replaced with the corresponding variable or constant from the outer scope.

static void Main(string[] args) {
    double price = 888 / GetCurrencyRate(Currency.JPY);
    // ...
}

Note

If the method is called with an expression as a parameter, the Inline Method and Delete refactoring introduces a local variable before inlining the method.

See Also