C# Language Reference
event (C# Reference)

The event keyword is used to declare an event in a publisher class.

Remarks

The following example shows how to declare and raise an event that uses EventHandler as the underlying delegate type. For the complete code example that also shows how to use the generic EventHandler<T> delegate type and how to subscribe to an event and create an event handler method, see How to: Publish Events that Conform to .NET Framework Guidelines (C# Programming Guide).

    public class Publisher
    {
        // Declare the delegate (if using non-generic pattern).
        public delegate void SampleEventHandler(object sender, SampleEventArgs e);

        // Declare the event.
        public event SampleEventHandler SampleEvent;

        // Wrap the event in a protected virtual method
        // to enable derived classes to raise the event.
        protected virtual void RaiseSampleEvent()
        {
            // Raise the event by using the () operator.
            SampleEvent(this, new SampleEventArgs("Hello"));
        }
    }

Events are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared (the publisher class). If other classes or structs subscribe to the event, their event handler methods will be called when the publisher class raises the event. For more information and code examples, see Events (C# Programming Guide) and Delegates (C# Programming Guide).

Events can be marked as public, private, protected, internal, or protected internal. These access modifiers define how users of the class can access the event. For more information, see Access Modifiers.

Keywords and Events

The following keywords apply to events.

Keyword Description For more information

static

Makes the event available to callers at any time, even if no instance of the class exists.

Static Classes and Static Class Members

virtual

Allows derived classes to override the event behavior by using the override keyword.

Inheritance (C# Programming Guide)

sealed

Specifies that for derived classes it is no longer virtual.

 

abstract

The compiler will not generate the add and remove event accessor blocks and therefore derived classes must provide their own implementation.

 

An event may be declared as a static event by using the static keyword. This makes the event available to callers at any time, even if no instance of the class exists. For more information, see Static Classes and Static Class Members.

An event can be marked as a virtual event by using the virtual keyword. This allows derived classes to override the event behavior by using the override keyword. For more information, see Inheritance (C# Programming Guide). An event overriding a virtual event can also be sealed, specifying that for derived classes it is no longer virtual. Lastly, an event can be declared abstract, meaning the compiler will not generate the add and remove event accessor blocks and therefore derived classes must provide their own implementation.

C# Language Specification

For more information, see the following sections in the C# Language Specification:

  • 1.6.6.4 Events

  • 7.13.3 Event Assignment

  • 10.7 Events

  • 13.2.3 Interface Events

See Also

Tasks

How to: Combine Delegates (Multicast Delegates)(C# Programming Guide)

Reference

C# Keywords
Modifiers (C# Reference)

Concepts

C# Programming Guide

Other Resources

C# Reference

Tags :


Community Content

twita
Event May Be Null

This line will throw an "Object reference not set to an instance of an object" error if there are no subscribers:


SampleEvent(this, new SampleEventArgs("Hello"));


A better example might look something like this:

    

if (SampleEvent == null)
{
return;
}

SampleEvent(this, new EventArgs());

Of course, this doesn't address concurrency problems, but it least it doesn't error out on the most common scenario.

Tags :

Page view tracker