User Id :    Password :      New Member   Forgot Password  
 
Code refactoring with Visual Studio 2010 Part-1
Description Visual studio 2010 is a Great IDE(Integrated Development Environment) and we all are using it in day by day for our coding purpose. There are many great features provided by Visual Studio 2010 and Today I am going to show one of great feature called for code refactoring.   No. of Views     3279
  Rating     0
Author Jalpesh Vadgama   Posted On     04 Jul 2011
Tags C# 4.0,Code Refactor,Visual Studio 2010    

Sample Code   Download Code

Visual studio 2010 is a Great IDE(Integrated Development Environment) and we all are using it in day by day for our coding purpose. There are many great features provided by Visual Studio 2010 and Today I am going to show one of great feature called for code refactoring. This feature is one of the most unappreciated features of Visual Studio 2010 as lots of people still not using that and doing stuff manfully. So to explain feature let’s create a simple console application which will print first name and last name like following.

 

And following is code for that.

using System;
 
namespace CodeRefractoring
{
class Program
{
   static void Main(string[] args)
   {
       string firstName = "Jalpesh";
       string lastName = "Vadgama";
 
       Console.WriteLine(string.Format("FirstName:{0}",firstName));
       Console.WriteLine(string.Format("LastName:{0}", lastName));
       Console.ReadLine();
   }
}
}

 

So as you can see this is a very basic console application and let’s run it to see output.

 

 


So now lets explore our first feature called extract method in visual studio you can also do that via refractor menu like following. Just select the code for which you want to extract method and then click refractor menu and then click extract method. Now I am selecting three lines of code and clicking on refactor –> Extract Method just like following.

 

 

Once you click menu a dialog box will appear like following.

 

As you can I have highlighted two thing first is Method Name where I put Print as Method Name and another one Preview method signature where its smart enough to extract parameter also as We have just selected three lines with console.writeline. One you click ok it will extract the method and you code will be like this.

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();
   }
}
}

 

So as you can see in above code its has created a static method called Print and also passed parameter for as firstname and lastname. Isn’t that great!!!. It has also created static print method as I am calling it from static void main. 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