Notes from. NET4.0 Object-Oriented Programming Basics
Tag: Multi-Channel delegate, generic delegate, Func series delegate, Action series delegate and MethodInvoker, Predicate <T> delegate
Delegated call list
Delegate variables can combine multiple methods and execute them in batches.
Combination Method: Use addition and subtraction Operators
The Delegate class defines a static GetInvocationList method to obtain the Delegate call list. This function can be used to understand the Delegate method set.
A multi-channel delegate variable can reference multiple methods and execute a multi-channel delegate variable. This will cause all methods contained in it to be executed sequentially.
The content of the delegate variable is "read-only". merging or splitting the delegate list only produces a new delegate call list.
Generic delegation:
Starting from. NET2.0, delegation began to support generic technology, that is, generic delegation.
A simple example of generic delegation:
public delegate T MyGenericDelegate<T>(T obj1,T obj2);
static int MyFunc(int val1,int val2)
{
return val1*val2;
}
void MainFormLoad(object sender,EventArgs e)
{
MyGenericDelegate<int> del;
del=MyFunc;
label1.Text=del(12,22).ToString();
}
You can see that the use of generic delegation is similar to that of common delegation. The difference is that you need to specify generic parameters when using generic delegation.
Func series delegation:
Definition of Func series delegation:
public delegate TResult Func<TResult>();
public delegate TResult Func<T,TResult>(T arg);
public delegate TResult Func<T1,T2,TResult>(T1 arg1,T2 arg2);
public delegate TResult Func<T1,T2,T3,TResult>(T1 arg1,T2 arg2,T3 arg3);
public delegate TResult Func<T1,T2,T3,T4,TResult>(T1 arg1,T2 arg2,T3 arg3,T4 arg4);
The last generic type parameter declared by the Func delegate is the return value type of the method received by the delegate. The previous generic parameter is the form parameter type of the method received by the delegate.
Examples of Func series delegation:
public delegate TResult Func<TResult>();
public delegate TResult Func<T,TResult>(T arg);
public delegate TResult Func<T1,T2,TResult>(T1 arg1,T2 arg2);
public delegate TResult Func<T1,T2,T3,TResult>(T1 arg1,T2 arg2,T3 arg3);
public delegate TResult Func<T1,T2,T3,T4,TResult>(T1 arg1,T2 arg2,T3 arg3,T4 arg4);
static int Add(int i,int j){return i+j;}
static int Multiply(int i,int j){return i*j;}
static int Process(Func<int,int,int> op,int[] numbers,int from,int to)
{
int result=numbers[from];
for(int i=from;i<=to;i++)
result=op(result,numbers[i]);
return result;
}
void MainFormLoad(object sender,EventArgs e)
{
int[] numbers=new int[7]{0,2,4,1,6,8,10};
label1.Text=Process(Add,numbers,0,3).ToString()+"\n";
label1.Text+=Process(Multiply,numbers,3,6).ToString();
}
Defines two methods: Add and Multiply, and an Array Processing Method Process. It uses the delegate method as the parameter to Process the array.
Delegation of Action series and MethodInvoker
All methods that can be received by the Func generic delegate have return values. The Action series delegate is to receive the system predefined delegate that returns the void.
public delegate void Action();
public delegate void Action<T>(T obj);
public delegate void Action<T1,T2>(T1 arg1,T2 arg2);
public delegate void Action<T1,T2,T3>(T1 arg1,T2 arg2,T3 arg3);
The usage is the same as that of Func.
The MethodInvoker delegate is located in System. Windows. Forms and is declared as follows:
public delegate void MethodInvoker();
It is equivalent to the Action delegate with no type parameters.
Predicate <T> delegate:
The Predicate <T> delegate often appears in the. NET base class library and is defined as follows:
public delegate bool Predicate<T>(T obj);
This delegate references a method to return the bool value. In actual development, the Prodicate <T> delegate variable is often used to reference a "judgment condition function ", the code written inside the judgment condition function indicates that the object referenced by the function parameter meets the conditions. If the condition is met, the function returns true, for example:
class MyClass
{
public int Value;
public string Info;
}
private static List<MyClass> GetMyClassList()
{
List<MyClass> lst = new List<MyClass>();
Random ran = new Random();
MyClass obj = null;
for (int i = 0; i < 10; i++)
{
obj = new MyClass();
obj.Value=ran.Next(1,100);
obj.Info="object "+i.ToString();
lst.Add(obj);
}
return lst;
}
static bool GreaterThan50(MyClass elem)
{
if (elem.Value > 50)
return true;
return false;
}
void MainFormLoad(object sender,EventArgs e)
{
Predicate<MyClass> pred = GreaterThan50;
List<MyClass> lst = GetMyClassList();
MyClass foundElement= lst.Find(pred);
if (foundElement != null)
Label1.Text = string. Format ("the matching object is found. Infomation = {0}, Value = {1} ", foundElement. Info, foundElement. Value );
else
Label1.Text = "no matching objects found ";
}
Generate a List <MyClass> and query the output information of the MyClass object whose Value is greater than 50.
A Find method is defined in the List of generic sets <T>:
public T Find(Predicate<T> match);
The Lis <T>. Find method traverses all objects in the Set, and calls this function as the real parameter of the "judgment condition function" referenced by Predicate <T>.
When you use a Predicate <T> delegate, the condition for its determination is generally "hard-coded externally" rather than specified by the data itself. Otherwise, such delegation is not suitable.
The following sections also show anonymous methods, Lambda expressions, and callbacks.