1. Why should I use a delegate?
The Commission of Life is to entrust others to help us to do one thing, the program is similar to the Delegation. Look at the following example
classClass1 {Static voidMain (string[] Args) {List<int> list =Newlist<int>(); List. ADD (1); List. ADD (2); List. ADD (3); List. ADD (4); List. ADD (5); //Filter Even even (list); Objectdumper.write (list); } //filter even numbers in a collection public Static voidEven (list<int>List) { for(inti =0; I < List. Count; i++) { if(list[i]% 2!=0) {list. RemoveAt (i); I--; } } } //Filter The odd number in the collection public Static voidODD (list<int>List) { for(inti =0; I < List. Count; i++) { if(list[i]% 2==0) {list. RemoveAt (i); I--; } } } }
Using the example above we found that the even () method and the Odd () method differ only by one line of code, and that the use of a delegate can be passed as a parameter, the benefit of which is to reduce coupling between programs and save Code.
The above code is modified as Follows:
classClass2 {//declaring a delegate public Delegate BOOLCal (intnum); Static voidMain (string[] Args) {List<int> list =Newlist<int>(); List. ADD (1); List. ADD (2); List. ADD (3); List. ADD (4); List. ADD (5); //Filter EvenCal cal =NewCal (even); MyCal (list, cal); Objectdumper.write (list); } public Static BOOLEven (intNum) { if(num%2==0) { return true; } return false; } public Static BOOLODD (intNum) { if(num%2!=0) { return true; } return false; } public Static voidMyCal (list<int>list,cal Cal) { for(inti =0; I < List. Count; i++) { if(cal (list[i])) {list. RemoveAt (i); I--; } } } }
The delegate describes the signature of the method (including the parameter type, number, return value), declares a delegate Cal, and, like the method we want to call, receives a parameter of type int and returns the bool Type.
public Delegate BOOL Cal (int num);
To create a delegate using the new keyword:
New Cal (even);
Finally call Cal to get a collection of all even items
MyCal (list, cal);
In the MyCal (list<int> list,cal Cal) method, We passed a delegate as a parameter, and the Cal describes the signature of the even or odd method, so that you can decide whether to filter an odd number or an even number at the time of the Call.
What's the difference between this and our direct call to the even or odd method, not more troublesome, and then look down:
We can change the filtering method to the following code:
Delegate (int i) { if20) { returntrue; } return false ; });
Because the Mycal method receives a delegate, we can use an anonymous delegate when we do not define even or odd.
This saves unnecessary code, makes the structure easier to look at, and does not require a predefined method, which we decide at the time of the Call.
. NET gives us a further simplification, using lambda expressions:
// Code Evolution // Lamada return 2 0 true false; });
i = input parameter (i) = = {return i% 2 = = 0? true:false;} is a lambda statement that identifies the even number of Filters.
The complete code is as Follows:
//declaring a delegate public Delegate BOOLCal (intnum); Static voidMain (string[] Args) {List<int> list =Newlist<int>(); List. ADD (1); List. ADD (2); List. ADD (3); List. ADD (4); List. ADD (5); MyCal (list, (i) = = {return i% 2 = = 0? True:false; }); Objectdumper.write (list); } public Static voidMyCal (list<int>list, Cal Cal) { for(inti =0; I < List. Count; i++) { if(cal (list[i])) {list. RemoveAt (i); I--; } } }
This is more simple than the beginning, no matter how to add a new filter, we only need to modify our lambda statement, the others do not need to change, this is the charm of the Delegation.
. NET (i) Delegate First: What is a delegate