C#系列学习笔记7:委托与事件

委托与事件

delegate

委托相当于c/c++中的函数指针,用于实现 event 和 回调函数,所有的委托都继承自System.Delegate

public delegate void Del(string message);   // 定义了一个可以调用任何形参为string、无返回的委托

public void Print(string message){
    Console.WriteLine( message );
}

Del handler1 = new Del(Print);   // 实例化方法1
Del handler2 = Print;            // 实例化方法2
handler1("This is the method 1");
handler2("This is the method 2");

委托的另一种用法是传递回调函数:

public static void MethodWithCallback(int param1, int param2, Del callback)
{
    ...
    ...
    callback("The number is: " + (param1 + param2).ToString());
}

MethodWithCallback(1, 2, handler);      // output: The number is: 3

委托可以用运算符"+" “+=“进行多播和组播,作用是一个委托同时调用多个方法。若要删除调用列表中的方法,使用”-“或”-=”

var obj = new ClassA();
Del d1 = obj.Method1;
Del d2 = obj.Method2;
Del d3 = Method;

Del allMethodsDelegate = d1 + d2 + d3;                  // 将按顺序执行d1,d2,d3
Del oneMethodDelgate = allMethodsDelegate - d2 - d3;    // 实际上就是d1
  • Action是无返回值的泛型委托。Action至少0个参数,至多16个参数,无返回值。
    Action < int, string > 表示有传入参数int,string无返回值的委托
  • Func是有返回值的泛型委托。Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void。
    Func < int > 表示无参,返回值为int的委托
    Func < object, string, int > 表示传入参数为object, string 返回值为int的委托
  • predicate是返回bool型的泛型委托。Predicate有且只有一个参数,返回值固定为bool。
    predicate < int > 表示传入参数为int 返回bool的委托
public void Print(string message){
    Console.WriteLine( message );
}

public string GetMsg(int num)
{
    return num.ToString();
}
        
Action<string> method1 = new Action<string>(Print);         // 使用Print
Func<string, int> method2 = new Fun<string, int>(GetMsg);   // 使用Func 
handler1("This is the method 1");
handler2("This is the method 2");

event

事件是对委托的封装

using System;
namespace SimpleEvent
{
  using System;
  /***********发布者 类***********/
  public class EventTest
  {
    private int value;
    
    public EventTest()
    {
      SetValue( n );
    }
    
    public delegate void handler();
    public event handler publisher;
    
    public void SetValue( int n )
    {
        value = n;
        if ( publisher != null ){
            publisher();                            /* 事件被触发 */
        }
        else {
            Console.WriteLine( "event not fire" );  /* 事件未触发 */
            Console.ReadKey();                      /* 回车继续 */
        }
    }

  }


  /***********订阅者 类***********/
  public class subscribEvent
  {
    public void printf()
    {
      Console.WriteLine( "event fire" );
      Console.ReadKey(); /* 回车继续 */
    }
  }

  /***********触发***********/
  public class MainClass
  {
    public static void Main()
    {
      EventTest e = new EventTest();            /* 实例化对象,第一次没有触发事件 */
      subscribEvent v = new subscribEvent();    /* 实例化对象 */
      e.publisher += new EventTest.handler( v.printf ); /* 注册,添加event到事件队列,按顺序进行处理 */
      e.SetValue( 7 );
      e.SetValue( 11 );
    }
  }
}

输出结果为:

event not fire
event fire
event fire

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值