Use an anonymous method as an event handler : Event : GUI Windows Forms C# Examples


C# Examples » GUI Windows Forms » Event »

 

Use an anonymous method as an event handler









    
using  System;    
    
delegate  void  MyEventHandler();    
    
class  MyEvent  {    
    public  event  MyEventHandler  SomeEvent;    
    
    public  void  OnSomeEvent()  {    
        if(SomeEvent  !=  null)    
            SomeEvent();    
    }    
}    
    
class  MainClass  {    
    public  static  void  Main()  {      
        MyEvent  evt  =  new  MyEvent();    
    
        //  Use  an  anonymous  method  as  an  event  handler.  
        evt.SomeEvent  +=  delegate    {    
            //  This  is  the  event  handler.  
            Console.WriteLine("Event  received.");    
        };  
  
        evt.OnSomeEvent();    
        evt.OnSomeEvent();    
    }    
}
    
   
  
   



Output

Event received.
Event received.


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» Event