Callback C # And JS synchronous and asynchronous implementation,
The code is very simple and I will not explain it much. If you have any questions or suggestions, please leave a message and reply immediately.
C # code first
Class Program {static void Main (string [] args) {MyCallback mc = new MyCallback (); mc. callback (new Action (success), new Action (failed); mc. callback (new Action () => {Console. writeLine ("No parameter anonymous success") ;}, new Action () =>{ Console. writeLine ("No parameter anonymous failed") ;}); mc. callback (new Action <object> (success), new Action <object> (failed); Console. readKey (); // from http://www.cnblogs.com/ahjesus Respect the author's hard work, and repost the source. Thank you!} Static void success () {Console. writeLine ("success");} static void failed () {Console. writeLine ("No parameter failed");} static void success (object str) {Console. writeLine (string) str);} static void failed (object str) {Console. writeLine (int) str) ;}} class MyCallback {public void Callback (Action success, Action failed) {success (); failed (); // from http://www.cnblogs.com/ahjesus Respect the author's hard work, and repost the source. Thank you!} Public void Callback (Action <object> success, Action <object> failed) {success ("123"); failed (456 );}}
JS Code
<Script type = "text/javascript"> var mc = new MyCallback (); mc. callback (success, failed); console. logs (222); function success (str) {console. log (str);} function failed (num) {console. log (num);} mc. callback (function (str) {console. log (str) ;}, function (num) {console. log (num); // from http://www.cnblogs.com/ahjesus Respect the author's hard work, and repost the source. Thank you! }) Console. logs (333); function MyCallback () {this. callback = function (success, failed) {// No parameter. If you have any parameter, choose not to pass the parameter successDelegate = success; // from http://www.cnblogs.com/ahjesus Respect the author's hard work, and repost the source. Thank you! FailedDelegate = failed; setTimeout ('successdelegate ("111") ', 1000); // asynchronous setTimeout ('faileddelegate ("444")', 4000 ); // asynchronous // success ("123"); // synchronous // failed (456); // synchronous }</script>