Skip to content

Commit a9e9d97

Browse files
authored
Add information about partial events & ctors (#45336)
* Add information about partial events & ctors * fix warnings * Final grammar pass Final grammar pass for partial events and constructors * Update docs/csharp/language-reference/keywords/event.md
1 parent e7daac8 commit a9e9d97

File tree

15 files changed

+161
-149
lines changed

15 files changed

+161
-149
lines changed

docfx.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
"field-keyword.md",
5858
"unbound-generic-types-in-nameof.md",
5959
"first-class-span-types.md",
60-
"simple-lambda-parameters-with-modifiers.md"
60+
"simple-lambda-parameters-with-modifiers.md",
61+
"partial-events-and-constructors.md"
6162
],
6263
"src": "_csharplang/proposals",
6364
"dest": "csharp/language-reference/proposals",
@@ -508,7 +509,7 @@
508509
"_csharplang/proposals/csharp-11.0/*.md": "09/30/2022",
509510
"_csharplang/proposals/csharp-12.0/*.md": "08/15/2023",
510511
"_csharplang/proposals/csharp-13.0/*.md": "10/31/2024",
511-
"_csharplang/proposals/*.md": "02/14/2025",
512+
"_csharplang/proposals/*.md": "03/11/2025",
512513
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "11/08/2022",
513514
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "11/08/2023",
514515
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "11/09/2024",
@@ -686,7 +687,8 @@
686687
"_csharplang/proposals/field-keyword.md": "The `field` contextual keyword",
687688
"_csharplang/proposals/unbound-generic-types-in-nameof.md": "Unbound generic types in `nameof`",
688689
"_csharplang/proposals/first-class-span-types.md": "First-class span types",
689-
"_csharplang/proposals/simple-lambda-parameters-with-modifiers.md": "Simple lambda parameters with modifiers",
690+
"_csharplang/proposals/simple-lambda-parameters-with-modifiers.md": "Simple lambda parameters with modifiers",
691+
"_csharplang/proposals/partial-events-and-constructors.md": "Partial events and constructors",
690692
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "C# compiler breaking changes since C# 10",
691693
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "C# compiler breaking changes since C# 11",
692694
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "C# compiler breaking changes since C# 12",
@@ -811,6 +813,7 @@
811813
"_csharplang/proposals/unbound-generic-types-in-nameof.md": "This proposal introduces the ability to use unbound generic types such as `List<>` in `nameof` expressions. The type argument isn't required.",
812814
"_csharplang/proposals/first-class-span-types.md": "This proposal provides several implicit conversions to `Span<T>` and `ReadOnlySpan<T>` that enable library authors to have fewer overloads and developers to write code that resolves to faster Span based APIs",
813815
"_csharplang/proposals/simple-lambda-parameters-with-modifiers.md": "This proposal provides allows lambda parmaeters to be declared with modifiers without requiring their type names. You can add modifiers like `ref` and `out` to lambda parameters without specifying their type.",
816+
"_csharplang/proposals/partial-events-and-constructors.md": "This proposal provides allows partial events and constructors to be declared in partial classes. This allows the event and constructor to be split across class declarations.",
814817
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 7.md": "Learn about any breaking changes since the initial release of C# 10 and included in C# 11",
815818
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 8.md": "Learn about any breaking changes since the initial release of C# 11 and included in C# 12",
816819
"_roslyn/docs/compilers/CSharp/Compiler Breaking Changes - DotNet 9.md": "Learn about any breaking changes since the initial release of C# 12 and included in C# 13",

docs/csharp/event-pattern.md

+7-8
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ void EventRaised(object sender, EventArgs args);
2222

2323
This standard signature provides insight into when events are used:
2424

25-
- ***The return type is void***. Events may have zero to many listeners. Raising an event notifies all listeners. In general, listeners don't provide values in response to events.
25+
- ***The return type is void***. Events can have zero to many listeners. Raising an event notifies all listeners. In general, listeners don't provide values in response to events.
2626
- ***Events indicate the sender***: The event signature includes the object that raised the event. That provides any listener with a mechanism to communicate with the sender. The compile-time type of `sender` is `System.Object`, even though you likely know a more derived type that would always be correct. By convention, use `object`.
27-
- ***Events package additional information in a single structure***: The `args` parameter is a type derived from <xref:System.EventArgs?displayProperty=nameWithType> that includes any additional necessary information. (You'll see in the [next section](modern-events.md) that this convention is no longer enforced.) If your event type doesn't need any more arguments, you still must provide both arguments. There's a special value, <xref:System.EventArgs.Empty?displayProperty=nameWithType> that you should use to denote that your event doesn't contain any additional information.
27+
- ***Events package more information in a single structure***: The `args` parameter is a type derived from <xref:System.EventArgs?displayProperty=nameWithType> that includes any more necessary information. (You'll see in the [next section](modern-events.md) that this convention is no longer enforced.) If your event type doesn't need any more arguments, you still must provide both arguments. There's a special value, <xref:System.EventArgs.Empty?displayProperty=nameWithType> that you should use to denote that your event doesn't contain any additional information.
2828

2929
Let's build a class that lists files in a directory, or any of its subdirectories that follow a pattern. This component raises an event for each file found that matches the pattern.
3030

@@ -48,22 +48,21 @@ The simplest way to add an event to your class is to declare that event as a pub
4848

4949
:::code language="csharp" source="./snippets/events/Program.cs" id="DeclareEvent":::
5050

51-
This looks like it's declaring a public field, which would appear to be a bad object-oriented practice. You want to protect data access through properties, or methods. While this code might look like a bad practice, the code generated by the compiler does create wrappers so that the event objects can only be accessed in safe ways. The only operations available on a field-like event are *add handler*:
51+
This looks like it's declaring a public field, which would appear to be a bad object-oriented practice. You want to protect data access through properties, or methods. While this code might look like a bad practice, the code generated by the compiler does create wrappers so that the event objects can only be accessed in safe ways. The only operations available on a field-like event are *add* and *remove* handler:
5252

5353
:::code language="csharp" source="./snippets/events/Program.cs" id="AttachEventHandler":::
5454

55-
And *remove handler*:
56-
5755
:::code language="csharp" source="./snippets/events/Program.cs" id="DetachHandler":::
5856

59-
There's a local variable for the handler. If you used the body of the lambda, the remove wouldn't work correctly. It would be a different instance of the delegate, and silently do nothing.
57+
There's a local variable for the handler. If you used the body of the lambda, the `remove` handler wouldn't work correctly. It would be a different instance of the delegate, and silently do nothing.
6058

6159
Code outside the class can't raise the event, nor can it perform any other operations.
6260

61+
Beginning with C# 14, events can be declared as [partial members](./language-reference/keywords/partial-member.md). A partial event declaration must include a *defining declaration* and an *implementing declaration*. The defining declaration must use the field-like event syntax. The implementing declaration must declare the `add` and `remove` handlers.
62+
6363
## Return values from event subscribers
6464

65-
Your simple version is working fine. Let's add another feature:
66-
Cancellation.
65+
Your simple version is working fine. Let's add another feature: Cancellation.
6766

6867
When you raise the *Found* event, listeners should be able to stop further processing, if this file is the last one sought.
6968

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
---
2-
description: Learn how to create custom event accessors using add keyword in C#
3-
title: "add keyword"
4-
ms.date: 07/20/2015
2+
description: The `add` contextual keyword declares an event accessor that adds a handler to that event.
3+
title: "The `add` keyword"
4+
ms.date: 03/13/2025
55
f1_keywords:
66
- "add_CSharpKeyword"
77
helpviewer_keywords:
88
- "add event accessor [C#]"
9-
ms.assetid: faf30b99-10e8-45cd-ab9a-57585d4d1d8d
109
---
11-
# add (C# Reference)
10+
# The `add` contextual keyword (C# Reference)
1211

13-
The `add` contextual keyword is used to define a custom event accessor that is invoked when client code subscribes to your [event](./event.md). If you supply a custom `add` accessor, you must also supply a [remove](./remove.md) accessor.
14-
15-
## Example
12+
The `add` contextual keyword is used to define a custom event accessor that is invoked when client code subscribes to your [event](./event.md). If you supply a custom `add` accessor, you must also supply a [remove](./remove.md) accessor.
1613

1714
The following example shows an event that has custom `add` and [remove](./remove.md) accessors. For the full example, see [How to implement interface events](../../programming-guide/events/how-to-implement-interface-events.md).
18-
19-
[!code-csharp[csrefKeywordsContextual#15](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsContextual/CS/csrefKeywordsContextual.cs#15)]
20-
21-
You do not typically need to provide your own custom event accessors. The accessors that are automatically generated by the compiler when you declare an event are sufficient for most scenarios.
22-
15+
16+
:::code language="csharp" source="./snippets/events.cs" id="AddHandler":::
17+
18+
You don't typically need to provide your own custom event accessors. The automatically generated accessors when you declare an event are sufficient for most scenarios. Beginning with C# 14, you can declare [`partial`](./partial-member.md) events. The implementing declaration of a partial event must declare the `add` and `remove` handlers.
19+
2320
## See also
2421

2522
- [Events](../../programming-guide/events/index.md)

docs/csharp/language-reference/keywords/event.md

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
---
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
55
f1_keywords:
66
- "event"
77
- "remove"
88
- "event_CSharpKeyword"
99
- "add"
1010
helpviewer_keywords:
1111
- "event keyword [C#]"
12-
ms.assetid: 7858fd85-153b-4259-85d0-6aa13c35f174
1312
---
14-
# event (C# reference)
13+
# The `event` keyword (C# reference)
1514

1615
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.
1716

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,
1918

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":::
2120

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).
2322

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).
2524

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.
2726

2827
## Keywords and events
2928

3029
The following keywords apply to events.
3130

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. | |
3837

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).
4039

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.
4241

4342
## C# language specification
4443

0 commit comments

Comments
 (0)