A static method is used as an event handler : Delegate event : Delegate C# Examples


C# Examples » Delegate » Delegate event »

 

A static method is used as an event handler









    
using  System;  
  
delegate  void  MyEventHandler();  
  
class  MyEvent  {  
    public  event  MyEventHandler  SomeEvent;  
  
    public  void  OnSomeEvent()  {  
        if(SomeEvent  !=  null)  
            SomeEvent();  
    }  
}  
  
class  KeyEvent  {  
  
    public  static  void  KeyEventHandler()  {  
        Console.WriteLine("Event  received  by  class.");  
    }  
}  
  
class  MainClass  {  
    public  static  void  Main()  {    
        MyEvent  evt  =  new  MyEvent();  
  
        evt.SomeEvent  +=  KeyEvent.KeyEventHandler;  
  
        evt.OnSomeEvent();  
    }  
}
    
   
  
   



Output

Event received by class.


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Delegate
» Delegate event