oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Refactoring in Whidbey
Pages: 1, 2, 3, 4

Reorder and Remove Parameters

You can reorder the parameters in a method. Consider the following method from an earlier example:

private static int Summation(int num, ref int sum) {
  int i;
  for (i = 1; i <= num; i++)
    sum += i;
  return i;

Position your cursor within the signature and right-click and select Refactor -> Reorder Parameters... (see Figure 8).

Figure 8. Reordering a parameter list

You can then rearrange the order of the parameter list (see Figure 9). All statements that call the modified method will have their arguments order changed.

Figure 9. The Reorder Parameters dialog

Besides reordering parameters, you can also remove parameters (see Figure 8, Remove Parameters). The current implementation only allows one parameter to be removed at a time. I do not find this feature useful, as it is much more efficient removing the parameters manually.


Renaming variables is one common task that programmers do. However, if you are not careful, you may inadvertently rename the wrong variable (most people use the find-and-replace feature available in the IDE, which is susceptible to wrongly renaming variables). In C# refactoring, you can rename variables by selecting a variable and right-clicking on it. Select Refactoring -> Rename... (see Figure 10).

Figure 10. Renaming a variable

You will be prompted for a new name (see Figure 11).

Figure 11. The Rename dialog

As usual, you have the chance to preview the changes (see Figure 12):

Figure 12. Previewing the changes for variables' renaming

Promote Local Variable to Parameter

You can also promote a local variable into a parameter. Consider the following example:

private static int Summation(int num) {

  int i, start=0, sum=0;

  for (i = start; i <= num; i++) {
    sum += i;

  return sum;

I want to promote the variable start into a parameter, so that callers of this function can initialize the start value. I select the variable start and right-click. I then select Refactor -> Promote Local Variable to Parameter (see Figure 13).

Figure 13. Promoting a variable to parameter

Note that the local variable to be promoted must be initialized, or else an error will occur. The promoted variable is now in the parameter list:

private static int Summation(int start, int num) {

  int i, sum = 0;

  for (i = start; i <= num; i++) {
    sum += i;

  return sum;

Surround With

A very useful refactoring feature in C# 2.0 is Surround With. You can use this feature to enclose a block of code with the selected construct. Consider the following example:

private void Form1_Load(object sender, EventArgs e) {
   string FILE_NAME=@"c:\textfile.txt";
   System.IO.StreamReader sr = new System.IO.StreamReader(FILE_NAME);
   string line = sr.ReadLine();
   while (line != null) {
      line = sr.ReadLine();

As performing I/O operations is one likely cause of system exceptions, it would be safer if you enclose the entire block of code within a Try-Catch block. Highlight the code that you want to enclose and right-click on it. Select Refactor -> Surround With... (See Figure 14).

Figure 14. Surrounding a block of code with a language construct

Pages: 1, 2, 3, 4

Next Pagearrow