其他分享
首页 > 其他分享> > EventHandler

EventHandler

作者:互联网

原文链接:http://www.cnblogs.com/leodrain/archive/2008/08/25/eventhandler-TEventArgs-to-declare-event-in-a-new-way.html

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

标签:EventHandler,FanStartedEventArgs,event,way,new,public
来源: https://blog.csdn.net/weixin_30855761/article/details/98999701