This is a simple delegate example
class TODO { public static void Main(String[] args) { Cal aa = new Cal(Add); (aa(2, 3)); } public delegate int Cal(int a, int b); private static int Add(int a, int b) { return a + b; } private static int Dec(int a, int b) { return a - b; } }
Such a simple application means function pointer
The entrustment means I give you the raw materials and the finished products I want. As for what you use my raw materials, I am not exploring them in depth.
So, there is a question, why don't I call it directlyAdd
orDec
What about the function?
This is the greatest benefit of commissioning.Variable separation, encapsulate the unchanged and isolate the changes. Examples are as follows:
class TODO { public static void Main(String[] args) { test(Add, 2, 3, 4); } public delegate int Cal(int a, int b); internal static void test(Cal cc, int a, int b, int c) { int x = 0; int y = 0; if (a > b) { if (a > c) { x = a; } else { if (b > c) { y = b; } else { y = c; } } } else { if (b > c) { x = b; } else { x = c; if (a > b) { y = a; } else { y = b; } } } int result = cc(x, y); (result); } private static int Add(int a, int b) { return a + b; } private static int Dec(int a, int b) { return a - b; } }
I have a plantest
Everything is fixed, but his calculation method is varied because we can use delegates to change the incoming method
So we don't changetest
In the case of this, he has more functions
Next is the event
This is the end of this article about in-depth understanding of C# commissions and events. For more related C# commissions and events, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!