Share the code in the test ~ Merge the methods that can be merged to Func.

Source: Internet
Author: User

In the object-oriented design, we often talk about "restructuring". In the reconstruction process, there is a merging method, that is, similar to, or, the method body is very long, so we need to consider them and abstract them!

Original code:

                                                       AddOpreation( a,  sum =  ( i = (a < b ? a : b); i <= (a > b ? a : b); i+++=                                               AddInvolutionOpreation( a,  sum =  ( i = (a < b ? a : b); i <= (a > b ? a : b); i+++= i *

We can see that the methods of the two methods are basically the same, but the first method is the accumulation of the original number, and the second method is the accumulation of the multiplication of the original number. For the above Code, we can abstract it again and it will be different.

The Code logic is proposed, we know. net 3.5 has two delegates: Action and Func. In fact, these two delegates are encapsulated by Microsoft for us. They are often used, so they are directly encapsulated, it is worth learning. Some people say that Microsoft is upgrading things quickly, but what I want to say is that its upgrading is not a language upgrade, but to make it easier for developers to use it

To make the code more readable and more artistic.

The following is the result of abstracting the changed parts and viewing the Code:

                                                                BothIntegerOpreation(Func<, > func,  a,  sum =  ( i = (a < b ? a : b); i <= (a > b ? a : b); i+++=

At the time of the call, we passed the specific implementation logic in the form of parameters. Some people say that the delegate is not used, but I want to say that you are not familiar with the delegate!

            Func<, > funcAdd = (a) => {  a; };             res1 = BothIntegerOpreation(funcAdd, ,  res2 = BothIntegerOpreation((a) => {  a * a; }, , );            Console.WriteLine(res1.ToString() + res2.ToString());

The result is as follows:

 

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.