委托的定义与委托变量的声明、初始化、赋值:
using System;
public class Test
{
delegate void TestDelegate(int a, int b);
static void Main()
{
//委托:将方法以变量的形式传递,且以方法的形式执行
TestDelegate D1 = Func1;
D1(1, 2);
D1 = Func2;
D1(3, 4);
//委托链:订阅与退订
TestDelegate D2 = null;
D2 += Func1;
D2 += Func2;
D2(5, 6);
D2 -= Func1;
D2(7, 8);
//匿名函数:一次性代码,不能订阅或者退订
TestDelegate D3 = delegate (int a, int b)
{
Console.WriteLine($"{a} * {b} = {a * b}");
};
D3(9, 10);
//lambda语句:比匿名函数更加简洁
TestDelegate D4 = (a, b) =>
{
Console.WriteLine($"{a} / {b} = {a / b}");
};
D4(12, 3);
//将委托作为方法的参数进行传递
Func3(Func1, 15, 3);
Func3(Func2, 20, 4);
}
static void Func1(int a, int b) => Console.WriteLine($"{a} + {b} = {a + b}");
static void Func2(int a, int b) => Console.WriteLine($"{a} - {b} = {a - b}");
static void Func3(TestDelegate myDelegate, int a, int b) => myDelegate(a, b);
}
运行结果如下:
1 + 2 = 3
3 - 4 = -1
5 + 6 = 11
5 - 6 = -1
7 - 8 = -1
9 * 10 = 90
12 / 3 = 4
15 + 3 = 18
20 - 4 = 16
如果定义的委托是有返回值类型的:
using System;
public class Test
{
delegate int Delegate(int n);
static void Main()
{
Delegate delegate1 = null;
delegate1 += Function1;
delegate1 += Function2;
Console.WriteLine(delegate1(1));//执行委托时,返回值为最后订阅的方法所产生的返回值
}
static int Function1(int n)
{
Console.WriteLine($"Result: {10 * n}");
return 10 * n;
}
static int Function2(int n)
{
Console.WriteLine($"Result: {100 * n}");
return 100 * n;
}
}
运行结果如下:
Result: 10
Result: 100
100
关于Action:
using System;
public class Test
{
static void Main()
{
//Action是System预定义的一种委托类型,要求无返回值且无参数
Action action1 = Function;
action1();
//Action<>可以传入若干个参数,但依然无返回值;它可以自动匹配方法的重载
Action<string, int, float> action2 = Function;
action2("YSY", 19, 1000);
}
static void Function() => Console.WriteLine("我没返回值也没参数");
static void Function(string name, int age, float salary) => Console.WriteLine("我叫" + name + ",我的岁数是" + age + ",我的工资是" + salary);
}
运行结果如下:
我没返回值也没参数
我叫YSY,我的岁数是19,我的工资是1000
delegate、event、Action、Func,你能弄清吗:
using System;
public class Test
{
//delegate
delegate void DelegateA();//定义委托
static DelegateA delegateA;//声明委托变量;全部声明为静态字段,方便使用
//event
delegate void DelegateB();//定义委托
static event DelegateB eventB;//声明委托变量;event是一个修饰符,表明该委托变量不为临时变量,且其只能在该类中被调用或赋值,在其他类中则只能订阅与退订
//Action:一定是无返回值的
static Action actionC;//声明委托变量;Action委托是预定义的
static Action<int> actionD;//声明委托变量;Action<int>委托是预定义的
//Func:一定是有返回值的;若<T1,T2,T3...>,则最后一个为返回值类型,前面依次是参数类型
static Func<int> funcE;//声明委托变量;Func<int>委托是预定义的
static Func<int, bool> funcF;//声明委托变量;Func<int, bool>委托是预定义的
static void Main()
{
delegateA += Function1;
delegateA();
eventB += Function1;
eventB();
actionC += Function1;
actionC();
actionD += Function2;
actionD(520);
funcE += Function3;
Console.WriteLine(funcE());
funcF += Function4;
Console.WriteLine(funcF(520));
}
static void Function1() => Console.WriteLine("Hello world!");
static void Function2(int a) => Console.WriteLine($"The number is {a}");
static int Function3() => 520;
static bool Function4(int a) => true;
}
运行结果如下:
Hello world!
Hello world!
Hello world!
The number is 520
520
True
不要说“事件”,只有“委托”,它只是被event修饰的委托而已:
using System;
public class Test
{
public event Action myEvent;//该委托变量可在别的类中进行方法的订阅与退订
public void OnMyEvent() => myEvent.Invoke();//相当于myEvent()
}
public class AnotherTest
{
static Test test = new Test();//声明为静态字段,因为要在静态的Main方法中调用
static void Main()
{
test.myEvent += Function;
test.OnMyEvent();
}
static void Function() => Console.WriteLine("Hello");
}
运行结果如下:
Hello