EventHandler

As it known to all  that Delegate and Event is two very important parts in .net ,and most of the beginners will be confused by the two and have the no idea of where to use and how to use them.

 

But in this article i don't want to discuss the base knowledge of   Delegate and Event, i just want to show us a new way to declare a Event which is not use Delegate.

 

In the first, i shall show the old way to do this,like the code slice following:

ContractedBlock.gifExpandedBlockStart.gifCode
public delegate void FanStartedEventHandler(object sender, FanStartedEventArgs e);
public event FanStartedEventHandler FanStarted;

 

and then used  by the observer

but when we user EventHandler<TEventArgs> we should do like this:

public class MyEventDemo
{
public event EventHandler<FanStartedEventArgs> FanStarted;

pulic 
void DoEvent()
{
   EventHandler
<FanStartedEventArgs> temp=FanStarted;
   
if(temp!=null)
   {
      temp(
this,new FanStartedEventArgs());
   }
}
}
public class FanStartedEventArgs
{}
public class Program
{
   
public static void Main()
   {
      MyEventDemo eventDemo 
= new MyEventDemo();
      eventDemo.FanStarted
+=new EventHandler<FanStartedEventArgs>(CallEventHandler)
     eventDemo.DoEvent();
    }
    
private static void CallEventHandler(object sender, FanStartedEventArgs e)
   {
    }
    
}

 

done

 

转载于:https://www.cnblogs.com/leodrain/archive/2008/08/25/eventhandler-TEventArgs-to-declare-event-in-a-new-way.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值