委托
语法
1 [访问修饰符] delegate 返回类型 委托名();
委托的特点
类似于 C++ 函数指针, 但它是类型安全的; 委托允许将方法作为参数进行传递; 委托可用于定义回调方法; 委托可以链接在一起: 如, 可以对一个事件调用多个方法.
委托在运行时确定调用哪种方法, 且委托和方法必须具有相同的签名.
- // 定义委托
- public delegate int Callback(int num1, int num2);
- // 定义方法类
- class MathCalc{
- // 加法方法
- public int Plus(int number1, int number2){
- return number1 + number2;
- }
- }
- static void Main(string[] args){
- // 委托的对象
- Callback objCall;
- // 实例 MathCalc 类的对象
- MathCalc mc = new MathCalc();
- // 将方法与委托关联起来
- objCall = new Callback(mc.Plus);
- // 可以用 Callback objCall = new Callback(mc.Plus); 声明委托并与方法关联
- // 将委托实例化
- int result = objCall(1, 2);
- System.Console.WriteLine("结果为{0}", result);
- }
匿名方法
委托能正常调用, 须创建单独的方法, 增加了实例化委托所需的编码系统开销. 为此可以使用匿名方法, 其不需要定义委托要引用的方法, 而把要引用的方法的方法体作为实例化委托时的一个代码块紧跟在委托后面.
语法
委托类型 委托实例 = delegate([参数列表]) {
- // 代码块
- }; // 分号不能少
使用方法
- class Program{
- // 定义委托
- delegate int delSum(int num1, int num2);
- static void Main(string[] args){
- int third = 3;
- // 将代码传递为委托参数
- delSum deladd = delegate(int first, int second){
- int sum = first + second;
- return sum;
- };
- int total = deladd(1, 2) + third;
- Console.WriteLine("1+2+3="+total);
- }
- }
匿名方法的特点
匿名方法的优点是减少了系统的开销, 方法仅在由委托使用时才定义. 匿名方法有助于降低代码的复杂性, 但如果委托代码块的代码很多则容易混淆代码, 降低程序的可读性.
在匿名方法中不能使用跳转语句跳转到匿名方法的外部, 同样匿名方法外部的跳转语句也不能跳转到匿名方法的内部. 匿名方法不能访问在外部使用 ref 和 out 修饰符的参数, 但可以访问在外部声明的其他变量.
事件
事件是一种特殊的委托, 在某件事情发生时, 一个对象可以通过事件通知另一个对象.
定义事件
为对象订阅该事件
将发生的事件通知给订阅人
- public class MeltdownEventArgs : EventArgs {
- private string message;
- public MeltdownEventArgs(string message) {
- this.message = message;
- }
- public string Message {
- get {
- return message;
- }
- }
- }
- public class Reactor {
- private int temperature;
- // 定义委托
- public delegate void MeltdownHandler(object reactor, MeltdownEventArgs MEA);
- // 定义事件
- public event MeltdownHandler OnMeltdown;
- public int Temperature {
- set {
- temperature = value;
- // 当温度值高于 1000 时, 创建叫 myMEA 的 MeltdownEventArgs 对象, 并把字符串传递进来给 myMEA 的 message 属性, 然后产生 OnMeltdown 事件, 把当前的 Reactor 对象和 myMEA 作为参数传递给 OnMeltdown
- if (temperature> 1000){
- MeltdownEventArgs myMEA = new MeltdownEventArgs("Reactor meltdown in progress!");
- OnMeltdown(this, myMEA);
- }
- }
- }
- }
- public class ReactorMonitor {
- //ReactorMonitor 构造函数接收 Reactor 对象 myReactor 为参数
- public ReactorMonitor(Reactor myReactor) {
- // 该构造函数为 OnMeltdown 事件监视 myReactor 对象. 当 myReactor 对象产生 OnMeltdown 事件时, DisplayMessage()方法会被调用来处理这个事件
- myReactor.OnMeltdown += new Reactor.MeltdownHandler(DisplayMessage);
- }
- public void DisplayMessage(object myReactor, MeltdownEventArgs myMEA) {
- Console.WriteLine(myMEA.Message);
- }
- }
- public static void Main(string[] args) {
- Reactor myReactor = new Reactor();
- ReactorMonitor myReactorMonitor = new ReactorMonitor(myReactor);
- Console.WriteLine("Setting reactor temperature to 100 degrees Centigrade");
- // 设置温度值为 100
- myReactor.Temperature = 100;
- Console.WriteLine("Setting reactor temperature to 2000 degrees Centigrade");
- // 设置温度值为 2000
- myReactor.Temperature = 2000;
- }
来源: http://www.bubuko.com/infodetail-2725353.html