Commissioned by the Popular edition

Source: Internet
Author: User

Commissioned by the Popular edition
Hypothesis

The weather is good today. It's very elegant...

You are in a good mood. You gave your wife 100.00, and your son 100.00 for consumption by themselves.

Then, you found your wallet empty. "My brother gave me 200 yuan in his mother's money and didn't let anyone live." He immediately called his son and wife, just keep on the phone, "beep, beep... ".

Then, there will be no more...

This is not the focus. As a naturally inspirational programmer, everything in the eyes is only code.

1 public static void Shop (string name, decimal total, Func <decimal, decimal> shopping) 2 {3 var balance = shopping (total); 4 Console. writeLine ("{0} remaining after shopping {1: C}", name, balance); 5}

Event 1: After the turtle son got the money

"Bad weather, old man, whole watermelon haha", so the son spent 20.5 yuan to buy a watermelon

        public static decimal BuyWatermelon(decimal total)        {            return total - 20.50m;         }
Shop ("turtle son", 100.00 m, BuyWatermelon );

Event 2: After you get the money

"It's hard to say that my son is not at home. I haven't been so embarrassed to buy it for a long time ...".

        public static decimal BuyDurex(decimal total)        {            return total - 10.00m;        }
Shop ("", 100.00 m, BuyDurex );

Seeing this, some bloggers are about to scold you. "If you are a zombie, you will write a story about your wife and son. If you roll back to the blog garden, do not pollute the garden ..."

This is not the point. The point is that a person gives his wife and son 100 yuan each to let them shop on their own, and then they can spend the money at will, only the remaining money is shown at the end.

In fact, delegation in code development is the same as that in reality. For example, if you want to buy food and ask your son to buy it, we will have:

You want to buy food (define the function "template ");

You know that your son will buy food (the son class defines the food buying function, but you don't know how to buy it. It's a car ride? Or walk? Which store did you go ?);

Then you entrust your son (replacing his food shopping behavior with your food shopping behavior). Now you can understand your son as a zombie;

Finally, the son buys the food (sometimes you need to pay attention to whether the son buys the food back, sometimes you don't even care about the time to come back, that is, whether or not there is a return value ).

Now let's look at the Code together. Of course, this example has not risen to the class level, which is a simple Program.

1 static void Main (string [] args) 2 {3 Shop ("turtle son", 100.00 m, BuyWatermelon); 4 Shop ("Mother-in-law", 100.00 m, BuyDurex ); 5 Console. readKey (); 6} 7 8 9 public static void Shop (string name, decimal total, Func <decimal, decimal> shopping/* consumption */) 10 {11 var balance = shopping (total); 12 Console. writeLine ("{0} remaining after shopping {1: C}", name, balance); 13} 14 15 public static decimal BuyWatermelon (decimal total) 16 {17 return total-20.50 m; 18} 19 20 public static decimal BuyDurex (decimal total) 21 {22 return total-10.00 m; 23}View Code

Seeing this, some bloggers began to roll back the "Internal Force, you can directly use the method, but also use a Mao delegate"

1 static void Main (string [] args) 2 {3 Shop ("turtle son", 100.00 m, 25.5 m); 4 Shop ("Mother-in-law", 100.00 m, 10 ); 5 Console. readKey (); 6} 7 8 public static void Shop (string name, decimal total, decimal shopping/* consumption */) 9 {10 var balance = total-shopping; 11 Console. writeLine ("{0} remaining after shopping {1: C}", name, balance); 12}

A: If your program is simply added or subtracted, and then you can print it, even the Shop method can be saved, you can simply output one sentence.

Console. WriteLine ("remaining {0: C}", 100.00 m-25.5 m after shopping );

Some bloggers opened the paper again. "That's what you mean, isn't it better to make the code more complicated ".

  

"Is there a gross relationship between good code and complexity ???"

...

If you have seen the cloud in the fog, and you don't know what fart things you 've talked about for half a day, okay, it doesn't matter. This is not the focus.

The key point is that ". Net provides a callback function mechanism by using delegation." You will know what the delegate is after one hundred times.

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.