I. Basic delegate writing
Internal class program {Private Static void main (string [] ARGs) {chaindelegate (); console. readkey ();} public static void chaindelegate () {// create a delegate feedback feedbacktoconsole = new feedback (feedbacktoconsole); feedback feedbacktomsbox = new feedback (feedbacktomsbox ); // feedbacktoconsole (1); feedbacktomsbox (2); // feedback fbchain = NULL; fbchain + = feedbacktoconsole; fbchain + = feedbacktomsbox; fbchain (3 );} public static void feedbacktoconsole (int32 value) {console. writeline (string. concat ("feedbacktoconsole->", value);} public static void feedbacktomsbox (int32 value) {console. writeline (string. concat ("feedbacktomsbox->", value) ;}// define a delegate internal delegate void feedback (int32 value );
View code
Knowledge point:
1. Define delegate: Delegate void feedbakc (int32 value)
- Delegate is a keyword.
- Void return value (various return values can be defined here, including generics)
- Int32 value defines the input parameters.
2. Define the callback function feedbacktoconsole and feedbacktomsbox
-
- The type and number of input parameters of the callback function must be exactly the same as that of the defined delegate.
- The return value is that the reference type must comply with the covariant and invert parameters (see the generic type I reproduced here). The value type does not need to comply with the covariant and invert parameters.
3. Create a delegate var feedbacktoconsole = new feedback (feedbacktoconsole)
- Use the New Keyword to create a delegate
- The parameter is your callback function (please refer to 2nd points)
4. Call the delegate
Feedbacktoconsole (1)
Feedbacktoconsole. Invoke (1)
-
- After a delegate is created, the corresponding Object Name (feedbacktoconsole) is equivalent to the delegate function (feedbacktoconsole ).
5. Delegated chain
Feed fbchain = NULL;
Fbchain + = feedbacktoconsole; fbchain = (feedback) Delegate. Combine (fbchain, feedbacktoconsole );
Fbchain + = feedbacktomsbox; fbchain = (feedback) Delegate. Combine (fbchain, feedbacktomsbox );
-
- Two writing methods are listed. I like the first one, which is simple and straightforward.
- Same as calling a single delegate chain, fbchain (3)
- The call sequence of the delegate chain is the same as that of the bound one (here, feedbacktoconsole is called first and then feedbacktomsbox is called)
6. Remove Data from the delegate chain (Supplement)
Fbchain-= feedbacktoconsole fbchain = (feedback) Delegate. Remove (fbchain, feedbacktoconsole );
- Ii. Delegate and Lamda expressions
Feedback fd1 = new Feedback(i => { Console.WriteLine(i); });
We use the Lamda expression to directly replace the callback function. If the callback function is not used in multiple places, I personally prefer this method, and I will write it as follows:
Feedback fd1 = new Feedback(Console.WriteLine);
This part requires some understanding of the Lamda expression.
- Iii. Generic Delegation
1. The generic delegation is the same as the generic interface. It involves covariant and inverter (here we can see the generic 3 I have reproduced)
2. You do not need to create generic delegation because C # provides two types of delegation:
Action <t,...>: only generic delegation with input parameters (with N reloads)
Func <in tinput,... out toutput>: generic delegation with input parameters and return values (with N reloads)
3. For more information about evenhandler, see [C #] events.