A class receives the notification when a static method is used as an event handler : delegate Event : Language Basics C# Source Code


Custom Search

C# Source Code » Language Basics » delegate Event »

 

A class receives the notification when a static method is used as an event handler









    

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


/* A class receives the notification when  
   a static method is used as an event handler. */ 
  
using System; 
 
// Declare a delegate for an event.  
delegate void MyEventHandler(); 
 
// Declare an event class. 
class MyEvent { 
  public event MyEventHandler SomeEvent; 
 
  // This is called to fire the event. 
  public void OnSomeEvent() { 
    if(SomeEvent != null) 
      SomeEvent(); 
  } 
} 
 
class X { 
 
  /* This is a static method that will be used as 
     an event handler. */ 
  public static void Xhandler() { 
    Console.WriteLine("Event received by class."); 
  } 
} 
 
public class EventDemo3 { 
  public static void Main() {  
    MyEvent evt = new MyEvent(); 
 
    evt.SomeEvent += new MyEventHandler(X.Xhandler); 
 
    // Fire the event. 
    evt.OnSomeEvent(); 
  } 
}


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Language Basics
» delegate Event