Code refactoring with Visual Studio 2010 Part-1

Posted by Jalpesh P. Vadgama on ASP.net Weblogs See other posts from ASP.net Weblogs or by Jalpesh P. Vadgama
Published on Sun, 26 Jun 2011 10:30:00 GMT Indexed on 2011/06/26 16:22 UTC
Read the original article Hit count: 514

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

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about c#.net