|
1 | 1 | ---
|
2 |
| -description: "event - C# Reference" |
3 |
| -title: "event keyword" |
4 |
| -ms.date: 07/20/2015 |
| 2 | +description: "Learn how to declare events with the `event` keyword - C# Reference" |
| 3 | +title: "The `event` keyword" |
| 4 | +ms.date: 03/13/2025 |
5 | 5 | f1_keywords:
|
6 | 6 | - "event"
|
7 | 7 | - "remove"
|
8 | 8 | - "event_CSharpKeyword"
|
9 | 9 | - "add"
|
10 | 10 | helpviewer_keywords:
|
11 | 11 | - "event keyword [C#]"
|
12 |
| -ms.assetid: 7858fd85-153b-4259-85d0-6aa13c35f174 |
13 | 12 | ---
|
14 |
| -# event (C# reference) |
| 13 | +# The `event` keyword (C# reference) |
15 | 14 |
|
16 | 15 | An ***event*** is a member that enables an object to trigger notifications. Event users can attach executable code for events by supplying ***event handlers***. The `event` keyword declares an ***event***. The event is of a delegate type. While an object triggers an event, the event invokes all supplied event handlers. Event handlers are delegate instances added to the event and executed when the event is raised. Event users can add or remove their event handlers on an event.
|
17 | 16 |
|
18 |
| -## Example |
| 17 | +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, see [How to publish events that conform to .NET Guidelines](/dotnet/standard/events). That sample demonstrates the generic <xref:System.EventHandler%601> delegate type, how to subscribe to an event, and create an event handler method, |
19 | 18 |
|
20 |
| -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](/dotnet/standard/events). |
| 19 | +:::code language="csharp" source="./snippets/Events.cs" id="EventExample"::: |
21 | 20 |
|
22 |
| -[!code-csharp[csrefKeywordsModifiers#7](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#7)] |
| 21 | +Events are multicast delegates that can only be invoked from within the class (or derived classes) or struct where they're declared (the publisher class). If other classes or structs subscribe to the event, their event handler methods are called when the publisher class raises the event. For more information and code examples, see [Events](../../programming-guide/events/index.md) and [Delegates](../../programming-guide/delegates/index.md). |
23 | 22 |
|
24 |
| -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](../../programming-guide/events/index.md) and [Delegates](../../programming-guide/delegates/index.md). |
| 23 | +Events can be marked as [`public`](./public.md), [`private`](./private.md), [`protected`](./protected.md), [`internal`](./internal.md), [`protected internal`](./protected-internal.md), or [`private protected`](./private-protected.md). These access modifiers define how users of the class can access the event. For more information, see [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md). |
25 | 24 |
|
26 |
| -Events can be marked as [public](./public.md), [private](./private.md), [protected](./protected.md), [internal](./internal.md), [protected internal](./protected-internal.md), or [private protected](./private-protected.md). These access modifiers define how users of the class can access the event. For more information, see [Access Modifiers](../../programming-guide/classes-and-structs/access-modifiers.md). |
| 25 | +Beginning with C# 14, events can be [`partial`](./partial-member.md). Partial events have one defining declaration and one implementing declaration. The defining declaration must use the field-like syntax. The implementing declaration must declare the `add` and `remove` handlers. |
27 | 26 |
|
28 | 27 | ## Keywords and events
|
29 | 28 |
|
30 | 29 | The following keywords apply to events.
|
31 | 30 |
|
32 |
| -|Keyword|Description|For more information| |
33 |
| -|-------------|-----------------|--------------------------| |
34 |
| -|[static](./static.md)|Makes the event available to callers at any time, even if no instance of the class exists.|[Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md)| |
35 |
| -|[virtual](./virtual.md)|Allows derived classes to override the event behavior by using the [override](./override.md) keyword.|[Inheritance](../../fundamentals/object-oriented/inheritance.md)| |
36 |
| -|[sealed](./sealed.md)|Specifies that for derived classes it is no longer virtual.|| |
37 |
| -|[abstract](./abstract.md)|The compiler will not generate the `add` and `remove` event accessor blocks and therefore derived classes must provide their own implementation.|| |
| 31 | +| Keyword | Description | For more information | |
| 32 | +|---------------------------|-----------------|----------------------| |
| 33 | +| [`static`](./static.md) | Makes the event available to callers at any time, even if no instance of the class exists. | [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md) | |
| 34 | +| [`virtual`](./virtual.md) | Allows derived classes to override the event behavior by using the [override](./override.md) keyword. | [Inheritance](../../fundamentals/object-oriented/inheritance.md)| |
| 35 | +| [`sealed`](./sealed.md) | Specifies that for derived classes it's no longer virtual. | | |
| 36 | +| [`abstract`](./abstract.md) | The compiler doesn't generate the `add` and `remove` event accessor blocks and therefore derived classes must provide their own implementation. | | |
38 | 37 |
|
39 |
| -An event may be declared as a static event by using the [static](./static.md) 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](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md). |
| 38 | +An event can be declared as a static event by using the [static](./static.md) keyword. Static events are available to callers at any time, even if no instance of the class exists. For more information, see [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md). |
40 | 39 |
|
41 |
| -An event can be marked as a virtual event by using the [virtual](./virtual.md) keyword. This enables derived classes to override the event behavior by using the [override](./override.md) keyword. For more information, see [Inheritance](../../fundamentals/object-oriented/inheritance.md). An event overriding a virtual event can also be [sealed](./sealed.md), which specifies that for derived classes it is no longer virtual. Lastly, an event can be declared [abstract](./abstract.md), which means that the compiler will not generate the `add` and `remove` event accessor blocks. Therefore derived classes must provide their own implementation. |
| 40 | +An event can be marked as a virtual event by using the [`virtual`](./virtual.md) keyword. Derived classes can override the event behavior by using the [`override`](./override.md) keyword. For more information, see [Inheritance](../../fundamentals/object-oriented/inheritance.md). An event overriding a virtual event can also be [`sealed`](./sealed.md), which specifies that for derived classes it's no longer virtual. Lastly, an event can be declared [`abstract`](./abstract.md), which means that the compiler doesn't generate the `add` and `remove` event accessor blocks. Therefore derived classes must provide their own implementation. |
42 | 41 |
|
43 | 42 | ## C# language specification
|
44 | 43 |
|
|
0 commit comments