Type and instance of delegate
1. Delegate type
A delegate type defines the class of methods that a delegate instance can call. Specifically, a delegate type defines the return types and parameters of a method.
delegate int Transformer(int x)
- Int Return type
- Transformer delegate type
- (int x) parameters
2. Delegate instance
Transformer t = Square ;
- T delegate instance
3, call
int answer = t(3); //answer is 9
2. Multicast delegation
A delegate instance can reference a set of target methods
1. Merge instance delegate
The + and += operators can merge delegate instances
SomeDelegate d = SomeMethod1;
d += SomeMethod2;
Copy the code
Third, the instance method target
When an instance method is assigned to a delegate object, the delegate object must retain a reference not only to the method but also to the instance to which the method belongs.
class Program
{
delegate void ProgressReporter(int x);
class X
{
public void InstanceProgress(int percentcomplete) => Console.WriteLine(percentcomplete);
}
static void Main(string[] args)
{
X x = new X();
ProgressReporter p = x.InstanceProgress;
p(99);
Console.WriteLine(p.Target == x); //True the instance to which the method belongs
Console.WriteLine(p.Method); / / Void InstanceProgress (Int32) methodConsole.ReadLine(); }}Copy the code
Generic delegate types
Delegate types can contain generic type parameters
public delegate T Transformer<T> (T arg);
- The return type is T and the argument is T
public delegate T Transformer<T>(T arg);
public class Util
{
public static void Transform<T>(T[] values, Transformer<T> t) // Arguments: an array, a delegate type
{
for (int i = 0; i < values.Length; i++) { values[i] = t(values[i]); }}}class Program
{
static void Main()
{
int[] values = { 1.2.3 };
Util.Transform<int>(values, Square); // Util.Transform(values, Square); You can abbreviate it this way, you can extrapolate from the parameters
// After the Transform call, values have changed
foreach (int i in values)
{
Console.Write(i + "");
}
Console.ReadLine();
}
static int Square(int x) => x * x;
}
Copy the code
Func and Action delegate
- Action has no return value
- Func has a return value
class Program { static void Main() { Action action1 = new Action(M1); action1(); Action<string, int> action2 = new Action<string,int>(SayHello); action2("zhonglq",18); Func<int, int, int> func_add = new Func<int, int, int>(Add); Console.WriteLine(func_add(2, 3)); //5 Func<double, double, double> func_mul = new Func<double, double, double>(Mul); Console.WriteLine(func_mul(2, 3)); //6 Console.ReadKey(); } static void M1() { Console.WriteLine("M1 is called"); } static void SayHello(string name,int round) { Console.WriteLine(name + round); } static int Add(int x,int y) { return x + y; } static double Mul(double x,double y) { return x * y; }}Copy the code
Lambda expressions
Intersection of two knowledge points
- Anonymous methods
- Inline methods (methods declared only when called)
A process of gradual simplification
Func<int, int, int> f = new Func<int, int, int>((a, b) => { return a + b; }); Console.WriteLine(f(5, 3)); Func<int, int, int> f1 = (a, b) => { return a + b; }; // new f1 = (x, y) => {return x * y; };Copy the code
Generic method + generic delegate type parameter + generic parameter
class Program { static void Main() { DoSomeCalc<int>((int a, int b) => { return a * b; }, 100, 200); DoSomeCalc<int>((a,b) => { return a * b; }, 100, 200); Console.readkey (); console.readkey (); console.readkey (); } static void DoSomeCalc<T>(Func<T,T,T> func,T x,T y) { T res = func(x, y); Console.WriteLine(res); }}Copy the code