Skip navigation

Refactoring: C#'s Cool New Feature

One of Microsoft's strategies in product releases seems to be introducing language-specific features that will be integrated across multiple languages in follow-on releases. For example, C# supports structured XML comments in Visual Studio 2003 and Visual Studio 2002. As part of Visual Studio 2005, Visual Basic (VB) will also include support for structured XML comments. So, just as VB has features that aren't in C#, this release of C# has a few features that aren't found in VB. For my money, refactoring is probably the most important of these, although this feature's nondescript name doesn't generate much interest. What is refactoring and why should you be interested in it?

Refactoring is basically a series of individual, logical changes to code—each of which is unlikely to cause bugs. By stringing together a series of changes, you make incremental improvements and test between each change so that you can quickly isolate any problems. In many ways, this process reflects how many of us already develop code: Make one change at a time, ensure that the necessary updates are propagated throughout your code, then test your code before making another change.

So how does Visual Studio 2005 support refactoring? Let's look at the basic example of changing a class name. After you rename a class, you must then go to each source file that references an instance of the class and update the class' name in that file. But with refactoring, you can ask Visual Studio to automatically go through your C# source files and rename all references to the class.

From a technical standpoint, refactoring is similar to what VB does for capitalization. Change "FOO" to "Foo," and VB automatically updates your code with the new capitalization. The difference is that if a VB developer changes "FOO" to "Bar," VB doesn't let you update your code with the new name. But C# does. This capability is the core of refactoring.

Visual Studio's new Compiler looks at the source code for all references it will compile based on the original name when you change a class or method's interface. Then, Visual Studio presents the references to you so that you can automatically update all your code. Gone are the days when a C# developer had to search source files for every instance of "FOO" to determine which of those instances to replace with "Bar."

If refactoring were only a fancy way to rename code, it wouldn't be a hot feature, but C# refactoring goes far beyond renaming. The feature also lets you add and reorder a method's parameter list. Do you need to change the order of parameters to reflect a parameter that's now optional? C# lets you automatically update all the code that calls your method to handle the new parameter order. Similarly, if you need to add a parameter, C#'s refactoring engine automatically adds a stub for that parameter in every existing call.

As you make simple changes to your source code, the C# refactoring engine lets you automatically propagate these changes across your source code. I think refactoring is one of the coolest C# features coming in Visual Studio 2005. I see only one problem with the current implementation of this feature: It hasn't been implemented in VB. Now, before you tell me this omission isn't a problem, consider that not every application is developed in just one .NET language. Many organizations use both VB and C# in their applications. However, now C# developers might develop a false sense of security as they refactor a method call to reorder parameters, only to find at runtime that they've failed to update any of the VB code another project in their solution uses. Unlike other features that Visual Studio 2005 includes, refactoring might create errors if C# developers refactor their code without updating and testing pathways that aren't part of the C# compiler's automated refactoring. The fact that Microsoft hasn't incorporated refactoring into VB seems like a waste to me because the VB environment already reviews your code for errors as you type. Microsoft could easily have implemented the same logic that VB uses for capitalization rules to handle the refactoring that C# will do in Visual Studio 2005. However, for now at least, refactoring will be available only to C# developers. For more information about the upcoming refactoring feature, see the Microsoft Developer Network (MSDN) Library article "Refactoring C# Code Using Visual Studio 2005" at

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.