Sunday, June 26, 2011

Code refactoring with Visual Studio 2010 Part-1

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.

ConsoleApplication

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.

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.

ExtractMethod

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

ExtractMethodDialog

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.

Shout it

kick it on DotNetKicks.com
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews