First review the use of the delegate process steps:
Summary of delegate use:
(1) Delegate declaration (Define a function prototype: return value + parameter type and number) Note: Outside the class-intermediary (real estate agent)
(2) According to the definition of "concrete" method —————— Listings Note: Define methods in a class
(3) Create a delegate object, associate a "concrete method" ——— The broker has listings note: Operate in the main function
The first way: Initialize with new. Second way: Assigning a method to a delegate variable directly
(4) Call the method by the delegate (instead of calling the method directly) —————— intermediary with people to see the house
Start learning: The idea of using a generic delegate through a requirement is: To find the maximum value of an arbitrary array
First of all, it's the usual method we used in the past: What method to write, and then call. We will not say much here, we should know .....
Second, use the delegate
Public Delegate intTomaxod (ObjectO1,ObjectO2); classProgram {Static voidMain (string[] args) { //The requirement is that the maximum value of an arbitrary array is implemented by a delegate Object[] Nu = {0,1,2,2,2,3,4 }; ObjectRe =Getmax (Nu, Tomaxok); Console.WriteLine ("max={0}", re); Console.ReadLine (); } Public Static ObjectGetmax (Object[] nums, Tomaxod del) { Objectmax = nums[0]; for(inti =0; I < Nums. Length; i++) { if(Del (max, nums[i]) <0) {Max=Nums[i]; } } returnMax; } Private Static intTomaxok (ObjectO1,ObjectO2) { intN1 = (int) O1; intN2 = (int) O2; returnN1-N2; } }
But this only implements the comparison of an integer array .
The string array is compared to (the annotation part is an integer array):
Public Delegate intTomaxod (ObjectO1,ObjectO2); Object for all ObjectsclassProgram {Static voidMain (string[] args) { //The requirement is that the maximum value of an arbitrary array is implemented by a delegate//object[] nus = {0, 1, 2, 2, 2, 3, 4}; //array of integral type//object re = Getmax (NUS, tomaxok);//to invoke a method that compares an array of integral types//Console.WriteLine ("Max={0}", re);
Object[] nus = {"ABC","Fdsfdsds","Fdsfdsfdsfdsfdsfds","Fdsfds"};//Array of strings Objectres =Getmax (NUS, TOMAXOK2);
Using anonymous functions to implement
Object res = Getmax (o, Delegate (object O1, Object O2) {
string S1 = (string) O1;
String s2 = (string) O2;
return S1. Length-s2. Length;
//});
Using lambda expressions to implement
Object res = Getmax (o, (object o1,object O2) + = {
string S1 = (string) O1;
String s2 = (string) O2;
return S1. Length-s2. Length;
//});
Console.WriteLine ("strmax={0}", RES); Console.ReadLine (); Console.ReadLine (); } Public Static ObjectGetmax (Object[] nums, Tomaxod del) { Objectmax = nums[0]; for(inti =0; I < Nums. Length; i++) { if(Del (max, nums[i]) <0) {Max=Nums[i]; } } returnMax; } Private Static intTOMAXOK2 (ObjectO1,ObjectO2)//string array type comparison {stringS1 = (string) O1; stringS2 = (string) O2; returnS1. Length-S2. Length; } //private static int Tomaxok (object O1, Object O2)//to implement a comparison of an integral type array//{ //int n1 = (int) O1; //int n2 = (int) O2; //return n1-n2; //} }
In the above example, we use object (representing all objects) in the filling parameters can not be explicitly required to fill in the type because it is the type of object, it is difficult to distinguish the specific type I need to fill in this time.
So, we consider what method can be implemented when filling out the automatic prompt I need to fill what type of object????
Using generic t to use generic delegates
The code that uses the generic delegate is:
Public Delegate intDelcompare<t>(t T1, T T2); //Public delegate int Delcompare (object O1, Object O2); classProgram {Static voidMain (string[] args) { //int[] Nums = {1, 2, 3, 4, 5}; //int max = getmax<int> (nums, Compare1); //Console.WriteLine (max); string[] names = {"ABCDEFG","Fdsfds","FDSFDSFDSFDSFDSFDSFDSFSD" }; stringMAX1 = getmax<string> (Names, (stringS1,stringS2) = { returnS1. Length-S2. Length; }); Console.WriteLine (MAX1); Console.readkey (); } Public StaticT getmax<t> (t[] nums, delcompare<t>del) {T Max= nums[0]; for(inti =0; I < Nums. Length; i++) { //to pass a comparison method. if(Del (max, nums[i]) <0) {Max=Nums[i]; } } returnMax; } //public static int Compare1 (int n1, int n2)//the specific int type is compared instead of the object type//{ //return n1-n2; //}}
You might not even think about it. Object is already a generic <T> why all types are represented???
Give a simple example of the:list<t> example
Like you're declaring a list<t>.
And then when you try to add a student object to the list
So this <T> is the equivalent of becoming <student> You can only insert Student objects into them
When you add another object to the list, you cannot insert it by default, although it inherits object but is not a student type.
But if you declare it in the first list<object>
Then you have no problem inserting any object, then you lose the meaning of the generic → canonical type.
Generic delegate-Learning process