Introduction to C# delegation (delegate, Action, Func, predicate)
A delegate is a class that defines the type of a method so that a method can be passed as an argument to another method. Events are a special kind of delegate.
1. Delegated Statement
(1). delegate
delegate我们常用到的一种声明
Delegate has at least 0 parameters and at most 32 parameters. It can have no return value or specify the return value type.
Example: public delegate int MethodtDelegate(int x, int y); means there are two parameters and returns int type.
(2). Action
Action是无返回值的泛型委托。
Action represents a delegate with no parameters and no return value
Action
Action
Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托
Action has at least 0 parameters, at most 16 parameters, and no return value.
example:
public void Test<T>(Action<T> action,T p)
{
action(p);
}
(3). Func
Func is a generic delegate with a return value
Func
Func
Func
Func
Func has at least 0 parameters and at most 16 parameters, and returns according to the return value generic type. There must be a return value, not void
例:
public int Test<T1,T2>(Func<T1,T2,int>func,T1 a,T2 b)
{
return func(a, b);
}
(4) .predicate
predicate is a generic delegate that returns bool
predicate
Predicate has one and only one parameter, and the return value is fixed to bool
例:public delegate bool Predicate
2. The use of delegation
(1). Use of Delegate
复制代码
复制代码
public delegate int MethodDelegate(int x, int y);
private static MethodDelegate method;
static void Main(string[] args)
{
method = new MethodDelegate(Add);
Console.WriteLine(method(10,20));
Console.ReadKey();
}
private static int Add(int x, int y)
{
return x + y;
}
Copy code
Copy code
(2). Use of Action
Copy Code
Copy Code
static void Main(string[] args)
{
Test
Test
Test
Console.ReadKey();
}
public static void Test
{
action(p);
}
private static void Action(string s)
{
Console.WriteLine(s);
}
private static void Action(int s)
{
Console.WriteLine(s);
}
Copy Code
Copy Code
Can use Action
(3). Use of Func
Copy Code
Copy Code
static void Main(string[] args)
{
Console.WriteLine(Test
Console.ReadKey();
}
public static int Test
{
return func(a, b);
} private static int Fun (
int a, int b) {
return
a + b;
Generic delegate: Represents a method that defines a set of conditions and determines whether a specified object meets those conditions. This delegate is used by several methods of the Array and List classes to search for elements in the collection.
复制代码
复制代码
static void Main(string[] args)
{
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
Point first = Array.Find(points, ProductGT10);
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
Console.ReadKey();
}
private static bool ProductGT10(Point p)
{
if (p.X * p.Y > 100000)
{
return true;
}
else
{
return false;
}
}
复制代码
复制代码
Search an array of Point structures using the Predicate delegate with the Array.Find method. The method ProductGT10 represented by this delegate will return true if the product of the X and Y fields is greater than 100,000. The Find method calls this delegate for each element of the array, stopping at the first point that meets the test criteria.
3. Delegate clearing
(1) Declare the clearing delegate method in the class, and remove the delegate reference in turn.
方法如下:
Copy code
Copy code
public MethodDelegate OnDelegate;
public void ClearDelegate()
{
while (this.OnDelegate != null)
{
this.OnDelegate -= this.OnDelegate;
}
}
Copy code
Copy code
(2). If no clear is declared in the class Delegate method, we can use GetInvocationList to query the delegate reference, and then remove it.
Methods as below:
Copy Code
Copy Code
public MethodDelegate OnDelegate;
static void Main(string[] args)
{
Program test = new Program();
if (test.OnDelegate != null)
{
System.Delegate[] dels = test.OnDelegate.GetInvocationList();
for (int i = 0; i < dels.Length; i++)
{
test.OnDelegate -= dels[i] as MethodDelegate;
}
}
}
Copy code
Copy code
4. Features of delegation
Delegates are similar to C++ function pointers, but they are type-safe.
Delegates allow methods to be passed as parameters.
Delegates can be used to define callback methods.
Delegates can be chained together; for example, multiple methods can be called on an event.
The method does not have to match the delegate signature exactly.
5. Summary:
Delegate has at least 0 parameters and at most 32 parameters. It can have no return value or specify the return value type.
Func can accept from 0 to 16 incoming parameters and must have a return value
Action can accept 0 to 16 incoming parameters, no return value
Predicate can only accept one incoming parameter, and the return value is bool type