Specifies an event.
[attributes] [modifiers] event type declarator; [attributes] [modifiers] event type member-name {accessor-declarations};
where:
The event keyword lets you specify a delegate that will be called upon the occurrence of some "event" in your code. The delegate can have one or more associated methods that will be called when your code indicates that the event has occurred. An event in one program can be made available to other programs that target the NGWS runtime.
The following steps must be taken in order to create and use C# events:
This class defines the event.
For more information on events, see:
A basic example of defining and using events can be found in the Event Tutorial.
It is also possible to declare an event in an interface and implement it in a class:
using System; public delegate void MyDelegate(); // delegate declaration public interface I { event MyDelegate MyEvent; void FireAway(); } public class MyClass: I { public event MyDelegate MyEvent; public void FireAway() { if (MyEvent != null) MyEvent(); } } public class MainClass { static private void f() { Console.WriteLine("This is called when the event fires."); } static public void Main () { I i = new MyClass(); i.MyEvent += new MyDelegate(f); i.FireAway(); } }
One use for event properties is for exposing a large number of properties without allocating a field for each event, but instead using a hash table to store the event instances. This is only useful if you have a very large number of events, but you expect most of the events will not be implemented.
using System; using System.Collections; public delegate void MyDelegate1(int i); public delegate void MyDelegate2(string s); public delegate void MyDelegate3(int i, object o); public delegate void MyDelegate4(); public class PropertyEventsSample { private Hashtable eventTable = new Hashtable(); public event MyDelegate1 Event1 { get { return (MyDelegate1) eventTable["Event1"]; } set { eventTable["Event1"] = value; } } public event MyDelegate1 Event2 { get { return (MyDelegate1) eventTable["Event2"]; } set { eventTable["Event2"] = value; } } public event MyDelegate2 Event3 { get { return (MyDelegate2) eventTable["Event3"]; } set { eventTable["Event3"] = value; } } public event MyDelegate3 Event4 { get { return (MyDelegate3) eventTable["Event4"]; } set { eventTable["Event4"] = value; } } public event MyDelegate3 Event5 { get { return (MyDelegate3) eventTable["Event5"]; } set { eventTable["Event5"] = value; } } public event MyDelegate4 Event6 { get { return (MyDelegate4) eventTable["Event6"]; } set { eventTable["Event6"] = value; } } } public class MyClass { public static void Main() { } }
Another use for event properties covers the situation where you are implementing two interfaces, each with an event of the same name. In such a case, you must use an explicit implementation event property:
using System; public delegate void MyDelegate1(); public interface I1 { event MyDelegate1 MyEvent; } public delegate int MyDelegate2(string s); public interface I2 { event MyDelegate2 MyEvent; } public class ExplicitEventsSample: I1, I2 { public event MyDelegate1 MyEvent; // normal implementation of I1.MyEvent. event MyDelegate2 I2.MyEvent { // explicit implementation of I2.MyEvent get { return MyEvent2Storage; } set { MyEvent2Storage = value; } } private MyDelegate2 MyEvent2Storage; // underlying storage for I2.MyEvent. private void FireEvents() { if (MyEvent != null) MyEvent(); if (MyEvent2Storage != null) MyEvent2Storage("hello"); } } public class MyClass { public static void Main() { } }