In C #, callback is implemented through delegation. The delegate provides us with a secure callback definition. Most delegates are related to events, but this is not all application scenarios of the Delegate. When classes need to communicate, in addition, if we want a mechanism that is more loosely coupled than interfaces, delegation is the best choice. The delegate allows us to configure the target at runtime and notify multiple customer objects. The delegate object contains a reference, which can be a static method or an instance method. With delegation, we can communicate with one or more customer objects that are linked at runtime.
Lambda expressions
Callback and delegation are very common in C #. C # provides a simplified syntax for delegation-lambda expressions. At the same time,. Net also has many common delegate forms built in, which are included in the three generic delegates:
- Predicate <t>: Indicates a function that provides the return value of bool to test a condition.
- Action <t>: Receives a series of parameters,But no value is returned..
- Func <t>:Receives a series of parametersReturns a single result.
These concepts are widely used in LINQ. List <t> also contains many callback methods, as follows:
1 Static Void Main (String [] ARGs) 2 { 3 List < Int > Numbers = enumerable. Range ( 1 , 200 ). Tolist (); 4 VaR Oddnumbers = numbers. Find (n => N % 2 = 1 ); // Returns the first matched element in the set. 5 VaR Test = numbers. trueforall (n => n < 50 ); // Whether or not each element in the Set meets the defined conditions. False is returned. 6 Numbers. removeall (n => N % 2 = 0 ); // Delete all even numbers 7 Numbers. foreach (item => console. writeline (item )); // Print all elements in the Set 8 9 Console. Read (); 10 }
All functions of LINQ depend on delegation. Callback is also used in the encapsulation of cross-thread calls in WPF and Windows Forms. In. NET Framework, the framework uses the delegate and allows callers to use lambda expressions.
Multicast Delegation
The delegates in C # Are all multicast delegates (Multicast delegate ). Multicast delegation combines all the target functions added to the delegate into a single call. Here we needNote::
1. If a delegate call encounters an exception, the delegate chain will be interrupted. This method cannot guarantee security.
2. the return value of the multicast delegate is the return value of the last function call on the delegate chain.
To solve the problem, we can manually traverse each delegate goal in the delegate chain (refer:Observer Pattern)), You can call the getinvocationlist () of the delegate instance to traverse each target in the delegate chain. See the following example: traversal can continue only when true is returned for each delegate call. Or we can add try/catch statement blocks when traversing the delegate chain to handle exceptions.
1 Public Void Lengthyoperation (func < Bool > Pred) 2 { 3 Bool Bcontinue = True ; 4 Foreach (Complicatedclass Cl In Container) 5 { 6 C1.dolengthyoperation (); 7 Foreach (Func < Bool > PR In Pred. getinvocationlist ()) 8 { 9 Bcontinue & = Pr (); // Equivalent to bcontinue = bcontinue & pr (); 10 11 If (! Bcontinue) 12 Return ; 13 } 14 } 15 }
Section
Delegation is the best way to perform callback at runtime. This method has Simpler requirements for the customer class, and you can configure the delegation target at runtime. In addition, the delegation also supports multiple customer targets. In. net, the client callback should be implemented using delegation.