Skip to main content

Inline Method

  • 2 minutes to read

Purpose

Replaces the current method call(s) with the method’s body without deleting the method. Use this Refactoring if you need to edit the method for the current call only and leave the rest of the code unchanged.

Availability

Available when the caret is on a method’s name within the method call or declaration. Executed from the method declaration, this Refactoring replaces all method calls with the method’s body.

Usage

  1. Place the caret on the 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 price1 = ConvertToUSD(Currency.EUR, 10);
        double price2 = ConvertToUSD(Currency.JPY, 888);
        // ...
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Inline Method from the menu.

After execution, the Refactoring replaces the method call with the method body contents. Each parameter reference in the method’s scope is replaced with the corresponding variable or constant from the outer scope.

public static double ConvertToUSD(Currency source, double amount) {
    return amount / GetCurrencyRate(source);
}
static void Main(string[] args) {
    double price1 = 10 / GetCurrencyRate(Currency.EUR);
    double price2 = ConvertToUSD(Currency.JPY, 888);
    // ...
}

Note

If the method was called with an expression as a parameter, the Refactoring introduces a local variable before inlining the method.

See Also