RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Code Refactoring in VS.NET 2005

Find out how to use VS.NET 2005's Refactoring engine to improve the structure and readability of your C# source code while preserving its functionality.

evelopment is often a messy process. Often, you need to organize your code to make it more readable, or restructure it to improve readability. Manually polishing code is both cumbersome and error prone. Fortunately,VS.NET 2005's Refactoring Tool is an awesome new feature that lets you modify your code without affecting its functionality—in other words, it helps you organize your C# code without changing the runtime outcome. This article shows you how to use the Refactoring Tool in Visual Studio.NET 2005 to refactor your C# code with ease.

According to MSDN:
"Refactoring is a formal and mechanical process, used to modify existing code in such a way that it does indeed become 'better' while preserving the program's intended functionality. In addition to improving a program's overall design, the refactoring process tends to yield code which is far easier to maintain and extend in the long run."
Editor's Note: Although it doesn't ship with the product, Microsoft supplies a free add-in for refactoring VB.NET code
The Refactor Menu
In Visual Studio.NET 2005, you can invoke refactoring either from the Visual Studio menu bar or from the context menu (see Figure 1).

Figure 1. The Refactor Menu: The figure shows the Refactor menu options.
The Refactor menu contains the following options:

  • Rename
  • Extract Method
  • Encapsulate Field
  • Extract Interface
  • Promote Local Variable to Parameter
  • Remove Parameters
  • Reorder Parameters
The following sections discuss how you can use each of these options of the Refactor menu.

The rename option lets you rename a namespace, type, field, variable, method, or method parameter seamlessly.

When you choose this option, you'll see the Rename dialog shown in Figure 2.

Figure 2. The Rename Dialog: When you select an item to rename, VS displays a dialog that lets you enter a new name, and (optionally) preview changes before applying them.
Figure 3. Preview Changes Dialog: You can get a good sense of how the change will affect the existing code by previewing the changes before applying them.
Figure 3 shows an example of the preview dialog that pops up after you click on the OK button in the Rename dialog in Figure 2.

Here, you can specify the new name for the token, preview changes, and even control the location where the changes would take effect.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date