
Refactoring and Renaming in C#
Refactoring is a software methodology that involves restructuring your code to make it easier to understand and to maintain, while preserving its functionality. A simple example might be that you write code in an event handler to get data from a database. As you develop your page, you discover that you need to access the data from several different handlers. Therefore, you refactor the page's code by creating a data-access function in the page and inserting calls to the function in the handlers.
The code editor includes tools to help you perform various refactoring tasks. In this walkthrough, you will work with two refactoring techniques: renaming symbols and extracting a method. Other refactoring options include encapsulating fields, promoting local variables to method parameters, and managing method parameters. The availability of these refactoring options depends upon the location in the code. For example, if you highlight code that is not a field declaration, you can not select the Encapsulate Field option. If you highlight a variable in an event method, you can not select Promote Local Variable to Parameter because event handler signatures are constant.
Refactoring Code
A common refactoring scenario is to create (extract) a method from code that is inside another member. This reduces the size of the original member and makes the extracted code reusable.
In this part of the walkthrough, you will write some simple code, and then extract a method from it. Refactoring is supported for C#, so you will create a page that uses C# as its programming language.
To create a C# page
-
In Solution Explorer, right-click the name of your Web site, and then click Add New Item.
-
Under Visual Studio installed templates, click Web Form.
-
In the Language list, click C#.
Note |
|---|
| You can leave the name Default2.aspx. |
-
Click Add to create and open the new page.
To extract a method in a C# page
-
Switch to Design view.
-
In the Toolbox, from the Standard group, drag a Button control onto the page.
-
Double-click the Button control to create a handler for its Click event, and then add the following highlighted code.
protected void Button1_Click(object sender, EventArgs e)
{
<b> System.Collections.ArrayList alist = </b>
<b> new System.Collections.ArrayList();</b>
<b> int i;</b>
<b> string arrayValue;</b>
<b> for(i=0; i<5; i++)</b>
<b> {</b>
<b> arrayValue = "i = " + i.ToString();</b>
<b> alist.Add(arrayValue);</b>
<b> }</b>
<b> for(i=0; i<alist.Count; i++)</b>
<b> {</b>
<b> Response.Write("<br>" + alist[i]);</b>
<b> }</b>
}
The code creates an ArrayList object, uses a loop to load it with values, and then uses another loop to display the contents of the ArrayList object.
-
Press CTRL+F5 to run the page, and then click the Button control to be sure that you see the following output:
i = 0
i = 1
i = 2
i = 3
i = 4
-
Return to the code editor, and then select the following lines in the event handler.
<b> for(i=0; i<alist.Count; i++)</b>
<b> {</b>
<b> Response.Write("<br>" + alist[i]);</b>
<b> }</b>
-
Right-click the selection, click Refactor, and then choose Extract Method.
The Extract Method dialog box appears.
-
In the New Method Name box, type DisplayArray, and then click OK.
The code editor creates a new method named DisplayArray, and puts a call to the new method in the Click handler where the loop was originally.
protected void Button1_Click(object sender, EventArgs e)
{
System.Collections.ArrayList alist =
new System.Collections.ArrayList();
int i;
string arrayValue;
for(i=0; i<5; i++)
{
arrayValue = "i = " + i.ToString();
alist.Add(arrayValue);
}
<b> i = DisplayArray(alist, i);</b>
}
-
Press CTRL+F5 to run the page again, and click the Button control.
The page functions the same as it did before.
Renaming Symbols
When working with variables and objects (both are also known as symbols), you might want to be able to rename the symbols after they are already referenced in your code. However, renaming the symbol can potentially cause the code to break if you miss renaming one of the references to the symbol. Therefore, you can use refactoring to perform the renaming.
To use refactoring to rename symbols
-
In the Click event handler, locate the following line:
System.Collections.ArrayList alist =
new System.Collections.ArrayList;
-
Right-click the variable name alist, choose Refactor, and choose Rename.
The Rename dialog box appears.
-
In the New name box, type ArrayList1, and then press ENTER.
The Preview Changes dialog box appears, and displays a tree containing all references to the symbol that you are renaming.
-
Click Apply to close the Preview Changes dialog box.
The variables that refer specifically to the instance that you selected are renamed. Note, however, that the variable alist in the following line is not renamed.
private int DisplayArray(System.Collections.ArrayList alist,
int i)
The variable alist in this line is not renamed because it does not represent the same value as the specific variable alist that you renamed. The variable alist in the DisplayArray declaration is a local variable for that method. This illustrates that using refactoring to rename symbols is different than simply performing a find-and-replace action in the editor; refactoring renames symbols with knowledge of the semantics of the symbol that it is working with.