C # delegated learning notes

Source: Internet
Author: User

It takes a long time to contact the delegated proxy delegate. Besides viewing APIs, kotoba also gave me a lecture. Speaking of delegation, it is better to take the following small example. (The exercises I spent on in June 26)

Example: Write a method A, define Method B (print Hello), and method C (print world), where I can only call method. Yes. Hello will be printed in 1 second, and world will be printed in 3 seconds.

Let's talk about the benefits of callback:

Some of our code is fixed during development, but some are flexible and easy to change. Let's assume that both B and C are not written by ourselves, but other methods that have been written by others. Therefore, we should try not to modify others' methods without any bugs, in this case, method A is written by ourselves. We need to call Method B in one second and method C in three seconds. This is the current method. Maybe, after my demand increases, more and more similar code will be available. For example, if method D is executed in 4 seconds, but one day you think method A is too slow to execute, you have changed the time to 0.5 seconds, and you have to change all the subsequent methods, this will be troublesome. Then we will make a slight modification, so that the modification time of Method B will not affect the execution of other methods.

 

Public Delegate void delemethod (); void start () {startcoroutine (FUNA (2, funb); startcoroutine (FUNA (3, func);} ienumerator FUNA (float time, delemethod m) {yield return New waitforseconds (time); m ();} void funb () {print ("hello");} void func () {print ("world ");}

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.