Promote Local Variable to Parameter

Promote Local Variable to Parameter is a Visual C# refactoring operation that provides an easy way to move a variable from a local usage to a method, indexer, or constructor parameter while updating the call sites correctly.

Perform the Promote Local Variable to Parameter operation by first positioning the cursor on the variable you want to promote. The statement declaring the variable must also assign a value or expression to the variable. When the cursor is in position, invoke the Promote Local Variable to Parameter operation by typing the keyboard shortcut, or by selecting the command from the shortcut menu.

When you invoke the Promote Local Variable to Parameter operation, the variable is added to the end of the parameter list for the member. Any calls to the modified member are immediately updated with the new parameter as the expression originally assigned to the variable, leaving the code so that it functions the same as before the variable promotion. For more information, see How to: Promote Local Variable to Parameter.

The following example shows the outcome of performing Promote Local Variable to Parameter on the variable fee.

Before

After

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Account Balance:  " + AddFee(100));
        Console.Read();
    }
    static public double AddFee(double accountbalance)
    {
        double fee = 5;
        return accountbalance + fee;
    }
}
class Program
{
    static void Main(string[] args)
        {
        Console.WriteLine("Account Balance:  " + AddFee(100, 5));
        Console.Read();
    }
    static public double AddFee(double accountbalance, double fee)
    {
        return accountbalance + fee;
    }
}

Remarks

This refactoring works best when the variable being promoted is assigned a constant value. The variable must be declared and initialized, not just a declaration or just an assignment.

See Also

Tasks

How to: Promote Local Variable to Parameter

Concepts

Refactoring