Refactoring C# Code Using Visual Studio 2010- Part 1

No.of Views2584
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  12 Jul 2011 08:07:45
Tag : Visual Studios , General
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
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

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

Image Loading

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.

Image Loading

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.

Image Loading

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

Image Loading

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

 
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