Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 3.95 KB

File metadata and controls

53 lines (38 loc) · 3.95 KB
description title ms.date f1_keywords helpviewer_keywords ms.assetid
event - C# Reference
event keyword
07/20/2015
event
remove
event_CSharpKeyword
add
event keyword [C#]
7858fd85-153b-4259-85d0-6aa13c35f174

event (C# reference)

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

Example

The following example shows how to declare and raise an event that uses xref:System.EventHandler as the underlying delegate type. For the complete code example that also shows how to use the generic xref:System.EventHandler%601 delegate type and how to subscribe to an event and create an event handler method, see How to publish events that conform to .NET Guidelines.

[!code-csharpcsrefKeywordsModifiers#7]

Events are a special kind of multicast delegate that can only be invoked from within the class (or derived classes) 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 and Delegates.

Events can be marked as public, private, protected, internal, protected internal, or private protected. 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
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 enables derived classes to override the event behavior by using the override keyword. For more information, see Inheritance. An event overriding a virtual event can also be sealed, which specifies that for derived classes it is no longer virtual. Lastly, an event can be declared abstract, which means that the compiler will not generate the add and remove event accessor blocks. Therefore derived classes must provide their own implementation.

C# language specification

[!INCLUDECSharplangspec]

See also