NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

event

Specifies an event.

[attributes] [modifiers] event type declarator;
[attributes] [modifiers] event type member-name {accessor-declarations};

where:

accessor-declarations
Declaration of the property accessors, which are used to read and write the property.
attributes (optional)
Optional declarative information. For more information on attributes and attribute classes, see C# Attributes.
declarator
The name of the event.
member-name
The name of the event property.
modifiers (optional)
Optional modifiers that include new, static, and one of the four access modifiers.
type
The delegate to which you want to associate this event.

Remarks

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:

  1. Create or identify a delegate. If you are defining your own event, you must also ensure that there is a delegate to use with the event keyword. If the event is predefined, in the base class library, for example, then consumers of the event need only know the name of the delegate.
  2. Create a class that contains:
    • An event created from the delegate.
    • (optional) A method that verifies that an instance of the delegate declared with the event keyword exists. Otherwise, this logic must be placed in the code that fires the event.
    • Methods that call the event. These methods can be overrides of some base class functionality.

    This class defines the event.

  3. Define one or more classes that connect methods to the event. Each of these classes will include:
    • A constructor declared to take a parameter of the type class that contains the event declaration. This constructor will contain the code to associate one or more methods with the event.
    • The definition of the method(s) that will be associated with the event.
  4. Use the event:
    • Create an object of the class that contains the event declaration.
    • Create an object of the class that contains the event definition, using the constructor that you defined.

For more information on events, see:

Example

A basic example of defining and using events can be found in the Event Tutorial.

Example

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();
   }
}

Example

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() {
   }
}

Example

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() {
   }
}

See Also

C# Keywords | Modifiers