Delegate (delegate)
Access modifier delegate return value type delegate name (parameter list)
A delegate is a type that can store a reference as a function, which means that it declares a data type that holds a particular format function, and a function pointer in C + +.
1. Anonymous delegation
Delegate type instance pseudonym = Delegate (argument list) {function Body}
2. Generic delegate
Delegate T1 delegate name <t1, t2> (T1 v1, T2 v2);
3. Delegation of Multicast
Delegate type instance alias + = Registration function
Delegate type instance alias-= Dismiss function
An instantiation delegate can not only register a function but also register multiple functions, and after registering multiple functions, each registration function will be executed sequentially according to the registration sequence of the registered function when the delegate is executed.
If the delegate has a return value, the return value of the last function is returned.
Note: If a delegate instance that has already registered a function is assigned from the new = sign, it is equivalent to being instantiated from the new instance.
Event
Access Modifier event delegate type events name
Events are similar to exceptions because they are raised by objects, and we can provide code to handle events. A single event can be subscribed to by multiple handlers, which are called when the program occurs.
Event Registration and deallocation function operations are the same as for delegates. In fact, an event is the encapsulation of a delegate, just as a property in a C # class encapsulates a field, and it encapsulates more complex logic on a delegate. After the event is compiled, a private delegate instance and two functions add_checkevent and remove_checkevent are generated automatically, respectively, which correspond to the +=/-= operation of the event.
1. Implicitly declaring an event
Event delegate type events name
2. Show declared events
Event delegate type events name
{
Add
{
Registering a function with a delegate instance of its own definition
}
Remove
{
To disassociate a function from registering a delegate instance of its own definition
}
}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceEvent {classDelegateTest { Public Delegate BOOLComparer (intV1,intv2); Publiccomparer Compare; Public BOOLMax (intV1,intV2) {returnV1 >v2;} Public BOOLMin (intV1,intV2) {returnV1 <v2;} /// <summary> ///Anonymous Delegate/// </summary> PrivateComparer Mycompare =Delegate(intV1,intv2) { returnV1 = =v2; }; /// <summary> ///Generic Delegate/// </summary> Public DelegateT2 _com<t1, t2>(T1 v1, T1 v2); Public voidSortint[] arr, comparer com) { for(inti =0; I < arr. GetLength (0); i++) { for(intj =0; J < arr. GetLength (0)-I-1; J + +) { if(!com (Arr[j], arr[j +1])) { intITemp =Arr[j]; ARR[J]= Arr[j +1]; Arr[j+1] =iTemp; } } } } }}
Client code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingsystem.timers;namespaceEvent {classProgram { Public Static EventDelegatetest.comparer Eventcomparer; Static voidMain (string[] args) { int[] arr =New int[] {1,2,3,4,6,7, the }; DelegateTest Test=Newdelegatetest (); Test.sort (arr, test. MAX); foreach(intVincharr) {Console.Write ("{0}", V); } ///////////////////////////////////////////////Console.WriteLine (); Test.compare=Hello; Test.compare+=World ; //a delegate with more than one function is registered, and the return value is the return value of the last run function. Console.WriteLine (Test.compare (1,2)); //Generic Delegate instantiationdelegatetest._com<int,BOOL> Compare =test. Max; ///////////////////////////////////////////////Eventcomparer =Hello; Eventcomparer+=World ; Eventcomparer (1,2); } Static BOOLHellointV1,intv2) {Console.Write ("Hello"); return false; } Static BOOLWorldintV1,intv2) {Console.WriteLine ("wrold!"); return true; } }}
C # delegates, events