static event

In this chapter you will learn:

  1. How to create static event

How to create static event

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

using System; /*from  ja v  a  2 s . c o m*/
 
delegate void MyEventHandler(); 
 
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(); 
  } 
}

Next chapter...

What you will learn in the next chapter:

  1. How to use EventHandler in C#
Home » C# Tutorial » delegate, lambda, event
delegate
Multicast delegate
delegate variables
delegate parameters
Generic delegate
delegate return
Func and Action
delegate new
chained delegate
Anonymous delegate
delegate array
Return a delegate
delegate as parameter
Event
event multicast
static event
EventHandler
Event pattern
lambda
lambda syntax
Func, Action and lambda
lambda with outer function
lambda iteration variables