A lot of complicated code can be commissioned to gracefully implement the same function.
Delegate: The Address method. NET version, similar to pointers in C + +, differs in type safety, the return type and parameter types that are set. A delegate is a reference to a method.
Defining delegates
Two parameter of type string, return value is String type
Public Delegate string GetString (string A,string b);
Action<t> and Func<t> commissioned
A generic action<t> delegate represents a method that references a void return type .
Func<t> allows you to invoke a method with a return type . Ps:func<int,int,bool> is a method that returns a Boolean type with two integer arguments
Situations where delegates are suitable for use
We use bubble sort to sort a set of int values, and the code is as follows
1 Public Static voidSort (int[] arr)2 {3 for(inti =0; I < arr. Length-1; i++)4 {5 for(intj =0; J < arr. Length-1I J + +)6 {7 if(Arr[j] > arr[j +1])8 {9 inttemp =Arr[j];TenARR[J] = arr[j +1]; OneArr[j +1] =temp; A } - } - } the}
View Code
But if we want the sort method to order any object, the current implementation is definitely not enough.
Like a student object that needs to be sorted by age
1 classStudent2 {3 PublicStudent (stringNameintAge )4 {5 This. Name =name;6 This. Age =Age ;7 }8 9 Public stringName {Get;Private Set; }Ten Public intAge {Get;Private Set; } One A Public Override stringToString () - { - return string. Format ("{0}, {1}", Name, age); the } - - Public Static BOOLCompareage (Student s1, Student S2)//Age Comparison Method - { + returnS1. Age <S2. Age; - } +}
It can be extended to a generic method sort<t> A comparison method is required to compare the parameters of two T types, and it is certainly not possible to use the ">" operator to compare this method, which can be referenced func<t1,t2,tresult> the delegate, where T1 , T2 the same type:func<t,t,bool>
1 Static Public voidSort<t> (ilist<t> Sortarray, func<t, T,BOOL>comparison)2 {3 4 for(inti =0; I < Sortarray.count-1; i++)5 {6 for(intj =0; J < Sortarray.count-1I J + +)7 {8 if(Comparison (sortarray[j +1], sortarray[j]))9 {TenT temp =Sortarray[j]; OneSORTARRAY[J] = sortarray[j +1]; ASortarray[j +1] =temp; - } - } the } - -}
Test Call Method:
1Student[] Students = { 2 NewStudent ("Curry", the),3 NewStudent ("Lebron", at),4 NewStudent ("Kobe", -), 5 NewStudent ("Yao", One)6 };7 Bubblesorter.sort (students, student.compareage);8 foreach(varStudentinchstudents)9 {Ten Console.WriteLine (student); One}
Delegates can also refer to multiple methods (multicast delegates ), but it is important to note that one of the methods is abnormal and the entire iteration stops;
An anonymous method is a delegate-creation method that directly assigns a body of methods to a delegate, starting with c#3.0 and using the LAMDA expression instead of an anonymous method;
Events are based on delegates and provide a publish/subscribe mechanism for delegates
"From yards to programmers" using delegates to make code more elegant