Code refactoring in C# with Visual Studio 2010 Part-2In previous post I have written about Extract Method Code refactoring option. In this post I am going to some other code refactoring features of Visual Studio 2010. Renaming variables and methods is one of the most difficult task for a developer. Normally we do like this. First we will rename method or variable and then we will find all the references then do remaining over that stuff. This will be become difficult if your variable or method are referenced at so many files and so many place. But once you use refactor menu rename it will be bit Easy. I am going to use same code which I have created in my previous post. I am just once again putting that code here for your reference.
using System;
namespace CodeRefractoring
{
class Program
{
static void Main(string[] args)
{
string firstName = "Jalpesh";
string lastName = "Vadgama";
Print(firstName, lastName);
}
private static void Print(string firstName, string lastName)
{
Console.WriteLine(string.Format("FirstName:{0}", firstName));
Console.WriteLine(string.Format("LastName:{0}", lastName));
Console.ReadLine();
}
}
}
Now I want to rename print method in this code. To rename the method you can select method name and then select Refactor-> Rename . Once I selected Print method and then click on rename a dialog box will appear like following.
Now I am renaming this Print method to PrintMyName like following.
Now once you click OK a dialog will appear with preview of code like following. It will show preview of code.
Now once you click apply. You code will be changed like following.
using System;
namespace CodeRefractoring
{
class Program
{
static void Main(string[] args)
{
string firstName = "Jalpesh";
string lastName = "Vadgama";
PrintMyName(firstName, lastName);
}
private static void PrintMyName(string firstName, string lastName)
{
Console.WriteLine(string.Format("FirstName:{0}", firstName));
Console.WriteLine(string.Format("LastName:{0}", lastName));
Console.ReadLine();
}
}
}
So that’s it. This will work in multiple files also. Hope you liked it.. Stay tuned for more.. Till that Happy Programming.