C# 委托 事件 匿名方法

(*) 委托

从最简单的例子开始:

namespace  ConsoleApplication1
{    
    
class  Program
    {
        
//  委托其实就相当于一个类型。这里,类型的名字叫BinaryOp
         public   delegate   int  BinaryOp( int  x,  int  y);

        
static   public   int  Add( int  x,  int  y)
        {
            
return  x  +  y;
        }

        
static   void  Main()
        {
            
//  创建一个BinaryOp类型的实例,用静态Add方法初始化
            BinaryOp d  =   new  BinaryOp(Add);

            Console.WriteLine(d(
10 10 ));
        }
    }
}
上面是静态方法的委托,下面再来看一个实例方法的委托。
class  MyClass
{
    
private   string  name;

    
public  MyClass( string  name)
    {
        
this .name  =  name;
    }

    
public   void  DisplayName() 
    {
        Console.WriteLine(
" {0} " , name);
    }
}

class  Program
{
    
//  委托其实就相当于一个类型。这里,类型的名字叫SimpleDelegate
     public   delegate   void  SimpleDelegate();        

    
static   void  Main()
    {
        MyClass a 
=   new  MyClass( " A " );
        MyClass b 
=   new  MyClass( " B " );
        
        
//  用实例方法DisplayName初始化
        SimpleDelegate d  =   new  SimpleDelegate(a.DisplayName);            
        d();

        d 
=   new  SimpleDelegate(b.DisplayName);
        d();
    }
}

 

(*) 事件
委托是个类型,而事件是个成员。看下面的代码:

namespace  ConsoleApplication1
{
    
public   class  SimpleMath
    {
        
public   delegate   int  BinaryOp( int  a,  int  b);  //  定义Binary类型
         public   event  BinaryOp BinaryEvent;  //  定义BinaryEvent成员

        
public   int  Add( int  a,  int  b) {  return  a  +  b; }
        
public   int  Substract( int  a,  int  b) {  return  a  -  b; }

        
public   int  Calculate() 
        {
            
//  Raise the event by using the () operator.
             return  BinaryEvent( 1 2 );  //  只能在定义事件的类的内部调用,如果写在外面会编译不过
        }
    }

    
class  Program
    {
        
static   void  Main() 
        {
            SimpleMath sm 
=   new  SimpleMath();

            
//  sm.BinaryEvent(1, 2); 编译错误!只能在定义事件的类的内部调用

            
//  下面两种注册方法效果是一样的,相当于注册了两遍,也的确会依序执行两遍
            sm.BinaryEvent  +=   new  SimpleMath.BinaryOp(sm.Add);
            sm.BinaryEvent 
+=  sm.Add;

            Console.WriteLine(sm.Calculate()); 
//  结果是3

            
//  下面两种注册方法效果是一样的,相当于注册了两遍,也的确会依序执行两遍
            sm.BinaryEvent  +=   new  SimpleMath.BinaryOp(sm.Substract);
            sm.BinaryEvent 
+=  sm.Substract;            

            Console.WriteLine(sm.Calculate()); 
//  -1, 只保留最后一次调用的返回值(3,3,-1,-1)
        }                
    }
}

 

(*) 匿名方法
匿名方法的作用是使代码更简化,方便了程序员。如果没有匿名方法,则定义一套事件监听需要这样:

class  Program
{
    
public   delegate   void  SomeDelegate();  //  定义委托
    
    
static   void  Main()
    {
        SomeType obj 
=   new  SomeType();
        obj.SomeEvent 
+=   new  SomeDelegate(MyEventHandler);
    }
    
//  一般来说,定义的MyEventHandler方法只用来响应事件,只在上面那一处地方使用
     public   static   void  MyEventHandler()  //  定义委托所调用的方法
    {}
}
上面的代码比较啰嗦,尤其是为委托和它所调用的方法起名字比较费劲我觉得。有了匿名方法以后,仅需要这样:
class  Program
{
    
static   void  Main()
    {
        SomeType obj 
=   new  SomeType();
        obj.SomeEvent 
+=   delegate {
            
//  实现事件处理逻辑
        };  //  注意要有分号
    }    
}
与上一段代码相比,省去了SomeDelegate和MyEventHandler的定义。

转载于:https://www.cnblogs.com/dc10101/archive/2009/03/19/1417209.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值