Refactoring C# Code Using Visual Studio 2010- Part II

No.of Views1535
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  12 Jul 2011 08:07:22
Tag : Visual Studios , General
In this article 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.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In previous article I have written about Extract Method Code refactoring option. In this article 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 article. 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.

Image Loading

Now I am renaming this Print method to PrintMyName like following.

Image Loading

Now once you click OK a dialog will appear with preview of code like following. It will show preview of code.

Image Loading

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.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Gold
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top