logo search
CSharp_Prog_Guide

Заметки

Эта операция оптимизации лучше всего действует в том случае, когда превращаемой переменной присваивается постоянное значение. Переменная должна объявляться и инициализироваться, это не должно быть только объявление или только присвоение значения.

How to: Promote Local Variable to Parameter

Use this procedure to perform the Promote Local Variable to Parameter refactoring operation.

To promote a local variable to parameter

  1. Create a console application, and set it up as described in the following example.

  2. Place the pointer next to i at its definition in MethodB.

  3. From the Refactor menu, select Promote Local Variable to Parameter.

You can also type the keyboard shortcut CTRL+R, P to complete the refactoring operation.

You can also right-click the pointer, point to Refactor on the context menu, and then click Promote Local Variable to Parameter to complete the refactoring operation.

The MethodB should now have a parameter int i, and the call ProtoA.MethodB will now pass zero as a value.

Example

To set up this example, create a console application named PromoteLocal, and then add the following code after the Program class in the PromoteLocal namespace.

class ProtoA

{

public static void MethodB()

{

// Invoke on 'i'

int i = 0;

}

}

class ProtoC

{

void MethodD()

{

ProtoA.MethodB();

}

}