Created
November 17, 2019 03:12
-
-
Save wantedfast/5a2bbac3f2f00b7881595d60a9455739 to your computer and use it in GitHub Desktop.
Learning notebook of C# Delegate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Advantage | |
| Decoupling the method | |
| Code a plugin method | |
| ### Code A Plugin Method | |
| public delegate int Transformer(int x); | |
| public static int GetNumber(int[] values, Transformer t){}; | |
| static void Main() | |
| { | |
| int[] vaules = {1,2,3}; | |
| GetNumber(values,square); | |
| } | |
| private static int square(int x) => x*x; | |
| ## Multiple Delegate | |
| SomeDelegate d = someMethod(); | |
| d += someMethod1(); | |
| d -= someMethod1(); // Remove the delegate. | |
| 如果多播委托的返回类型不是VOID, 那么调用者就会从最后一个调用者的方法中取得返回值,之前的返回值都会被弃用。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment