User Id :    Password :      New Member   Forgot Password  
 
Code refactoring in C# with Visual Studio 2010 Part-2
Description Code refactoring in C# with Visual Studio 2010 Part-2   No. of Views     1958
  Rating     0
Author Jalpesh Vadgama   Posted On     21 Jul 2011
Tags C# 4.0,Code Refactor,Visual Studio 2010    

Sample Code   Download Code

In 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.

 

 

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code