EventHandler declares a Event in a new way without delegate

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:

Code

 

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

 

posted @ 2008-08-25 22:45  Async Liu  阅读(435)  评论(1编辑  收藏  举报