Refactoring In Visual Studio 2008
While there is a lot of talk about refactoring tools such as Resharper and Refactor Pro, not much is said about refactoring features available in Visual Studio 2008 straight out of the box. I think Resharper and Refactor Pro are fantastic tools but they cost money and most of the time our refactoring needs are simple or you maybe in an environment where such tools are not available. So what do you do? Worry not, Visual Studio comes with excellent refactoring support. In this post I will show different refactoring features of Visual Studio.
Refactoring can be accessed either by going to Refactor menu or by right clicking on the text editor. Visual Studio comes with 7 refactoring options as seen below.

I will now show you how some of these refactorings work. I will work with this simple piece of code to illustrate simple refactorings. I will use another code sample for some refactorings. You will see it when it is required.
static void Main(string[] args)
{
string msg = "Hello";
if (msg != string.Empty)
{
Console.WriteLine(msg);
}
}
Rename
This is perhaps simplest of all. It renames a member. This refactoring comes in very handy and is most likely one of the most used refactoring. It can rename a member within scope. Here I have few lines of code with a member declared as msg. To rename the member I just need to put my cursor on the member name and click on Refator –> Rename menu or hit F2.

I can also include comments and/or strings in my refactor operation. Here I will ask msg to be renamed to message. Clicking OK on the dialog above opens another dialog where I can preview my changes.

Extract Method
Extract Method extracts a method out of one or many statements. I can extract a method for the code within if statement. I am presented with a dialog which asks me the name of my new method. It is also smart enough to figure out that I need a parameter passed into this method.

Resulting code now looks like this.
class Program
{
static void Main(string[] args)
{
string msg = "Hello";
PrintToConsole(msg);
}
private static void PrintToConsole(string msg)
{
if (msg != string.Empty)
{
Console.WriteLine(msg);
}
}
}
Encapsulate Field
From here on I will use a different code sample. This code sample is a customer object with two variables and a method.
public class Customer
{
private string name;
private string address;
public void PrintCustomerInformation()
{
string result =
string.Format("Name = {0}\nAddress = {1}\n", Name, Address);
}
}
This refactoring converts a private variable into a public property. By running Encapsulate Field on the private name variable presents this screen.

Once again I get a preview of changes which will be made.

I will also apply Encapsulate Field to address variable. The resulting code is modified to this.
public class Customer
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
public void PrintCustomerInformation()
{
string result =
string.Format("Name = {0}\nAddress = {1}\n", name, address);
}
}
Extract Interface
Extract Interface as the name says will extract an interface based on the members I select. By running Extract Interface I am presented with a dialog where I can select the members I wish to include in my interface definition. For this example I will include all Address, Name and PrintCustomerInformation members. I will also name my interface ICustomer.

By applying this refactoring Visual Studio creates another file for ICustomer interface. The resulting code of both files looks like this.
interface ICustomer
{
string Address { get; set; }
string Name { get; set; }
void PrintCustomerInformation();
}
public class Customer : RefactoringDemo.ICustomer
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string address;
public string Address
{
get { return address; }
set { address = value; }
}
public void PrintCustomerInformation()
{
string result =
string.Format("Name = {0}\nAddress = {1}\n", name, address);
}
}
Conclusion
Visual Studio refactoring tools may not be at the level of Resharper or Refactor Pro but they are powerful enough for most refactoring jobs. I have demonstrated some of these here. If you are interested in learning more about refactoring then I highly recommend this classic book.
![]() |
Refactoring: Improving the Design of Existing Code (The Addison-Wesley Object Technology Series)
by Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts |
6 Responses to Refactoring In Visual Studio 2008
Leave a Reply Cancel reply
Top Posts
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Windows 7 Control Panel In Classic Mode
- More Details Emerge On Microsoft Master Certification
- Use SqlConnection With LINQ To SQL
- Free Icons And Images With Visual Studio 2008
- Capture XML In WCF Service
- Dynamic Sort With LINQ
- StyleCop Tutorial
Tags
.Net 2010 ADO.NET ASP.NET Azure Blogging Books Browsers C# Certification Cloud Computing Code Snippets Community Data Services Eclipse Entity Framework Google IDE Java LINQ Mac Microsoft Museum NetBeans Office Oracle REST SharePoint Silverlight SQL Server T-SQL Tips Tools Training Visual Studio Visual Studio 2010 WCF Web Windows Windows 7 Windows Forms Windows Live WMI WPF XAML



Hi,
I’m just getting started with my new blog. Would you want to exchange links on our blog-rolls?
BTW – I’m up to about 100 visitors per day.
Dan,
Thanks for the offer. This blog is about Microsoft .Net which is not related to the main topic of your blog. Thus I will pass on your offer.
I wish you all the best with your blog.
most of the time our refactoring needs are simple
Ha! I disagree completely. Most of the time *my* refactoring needs are far more complex than even ReSharper can satisfy.
I do not see the Refactor menu. Is it not there for C++?
Michael,
I have not touched C++ since year 2000. Sorry I cannot confirm if refactor menu is available under C++
I can assure you that it is not available for C++. You have to use Refactor or similar plugins.