C # extraction method reconstruction,

Source: Internet
Author: User

C # extraction method reconstruction,

Reference: https://msdn.microsoft.com/zh-CN/library/0s21cwxk.aspx

"Extraction Method"Is a refactoring operation that provides a convenient way to create a new method from the code snippet of an existing member.

Use"Extraction Method"You can create a method by extracting a group of code from the code block of an existing member. The extracted new method contains the selected code, and the selected code in the existing member is replaced with the call to the new method. By changing a code segment into its own method, you can quickly and accurately re-organize the code to achieve better reusability and reliability.

"Extraction Method"It has the following advantages:

  • Best coding practices are encouraged by emphasizing discrete reusable methods.

  • We recommend that you use better organizations to obtain self-recorded code.

    When descriptive names are used, high-level methods can be read like reading a series of comments.

  • Create refinement methods to simplify overloading.

  • Reduce code duplication.

1. Create a nameExtractMethodConsole application, and then use the following code example to replace Program.

class A{    const double PI = 3.141592;    double CalculatePaintNeeded(double paintPerUnit, double radius)    {        // Select any of the following:        // 1. The entire next line of code.        // 2. The right-hand side of the next line of code.        // 3. Just "PI *" of the right-hand side of the next line        //    of code (to see the prompt for selection expansion).        // 4.  All code within the method body.        // ...Then invoke Extract Method.        double area = PI * radius * radius;        return area / paintPerUnit;    }}

2. Select the code snippet you want to extract:

double area = PI * radius * radius;

3."Refactoring"Click"Extraction Method".

Appears"Extraction Method"Dialog box.

You can also enter the keyboard shortcut Ctrl + R, Ctrl + M to display"Extraction Method"Dialog box.

You can also right-click the selected code and point"Refactoring"And then click"Extraction Method"To display"Extraction Method"Dialog box.

4."New Method Name"Specify the name of the new method, as shown in figureCircleArea.

The new method signature is previewed on"Preview method signature".

5. Click"OK"

Note:

Use"Extraction Method"Command, a new method will be inserted after the source member in the same class.

Division type

If the class is of the division type"Extraction Method"A new method is generated immediately after the source member."Extraction Method"Determine the signature of the new method, and create a static method when the code in the new method does not reference instance data.

Generic parameters

When the extracted method has an unrestricted generic type parameter, the generated code will not be added to this parameter unless it has been assigned a value.RefModifier. If the extracted method supports the reference type as the generic type real parameter, manually add the parameter to the method signature.RefModifier.

Anonymous Method

If you try to extract part of an anonymous method (this method includes a reference to a local variable declared or referenced outside the anonymous method), Visual Studio warns you of possible semantic changes.

When the anonymous method uses the value of a local variable, the value is obtained when the anonymous method is executed. When an anonymous method is extracted to another method, the value of the local variable is obtained when the extraction method is called.

The following example illustrates this semantic change. If this code is executed, it will be output to the console11. If you use"Extraction Method"Extract the code area marked by the code comment to its own method, and then execute the restructured code, it will be output to the console10.

class Program{    delegate void D();    D d;    static void Main(string[] args)    {        Program p = new Program();        int i = 10;        /*begin extraction*/            p.d = delegate { Console.WriteLine(i++); };        /*end extraction*/        i++;        p.d();    }}

To solve this problem, make the local variables used in the anonymous method a field of the class.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.