Monday, August 10, 2015

Extract Method Refactoring in C#

When to use this:
  1. Length of the method is not an issue
  2. To improve clarity of the code
  3. The name of the new method will reveal the intention of the code in a better way. If you can't come up with a more meaningful name, don't extract the code
       Example : Suppose we have method to use this extract method. by selecting console.WriteLine statement as below

        public void Add(int x, int y)
        {
            int z = x + y;
            Console.WriteLine(z);

        }

We can also right-click the selected code, point to Refactor, and then click Extract Method (Short cut  is CTRL+R, M)  to display the Extract Method dialog box.

The final extracted method is as follows

No comments:

Post a Comment