Skip to main content

Promote to Parameter

  • 2 minutes to read

Purpose

Converts a local variable to the method parameter. Use this Code Provider inside a method to generalize it. Having a new parameter on a method will increase its flexibility for consumers.

Note

Promote to Parameter is a cascading Code Provider. That means that the Code Provider affects all method calls and method declarations in interfaces, base and descendant classes.

Availability

Available when the caret is on a local variable name within the variable declaration statement.

Usage

  1. Place the caret on the name of a local variable in its declaration.

    Note

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

    public static double EURToUSD(double amount) {
        Currency source = Currency.EUR;
        return amount / GetCurrencyRate(source);
    }
    static void Main(string[] args) {
        double price = EURToUSD(10);
    }
    
  2. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  3. Select Promote to Parameter from the menu.

After execution, the Code Provider removes the variable initialization, adds the new method parameter named after the variable and adds its initial value to each method call.

public static double EURToUSD(double amount, Currency source) {
    return amount / GetCurrencyRate(source);
}
static void Main(string[] args) {
    double price = EURToUSD(10, Currency.EUR);
}

In the example above, the EURToUSD function has been generalized and can be renamed to ConvertToUSD.

See Also