Skip to content

Commit 458b992

Browse files
authored
changed files by pdets auto publish service, publishid[9b3c92d9-f950-4cfb-8e6b-3c9597e3eefa] and do [publish].
1 parent f2dfc41 commit 458b992

10 files changed

+37
-32
lines changed

learn-pr/wwl-language/create-manage-events/6-exercise-events-csharp.yml renamed to learn-pr/wwl-language/create-manage-events/6-exercise-implement-events-csharp.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### YamlMime:ModuleUnit
2-
uid: learn.wwl.create-manage-events.exercise-events-csharp
2+
uid: learn.wwl.create-manage-events.exercise-implement-events-csharp
33
title: 'Exercise: Implement events in C#'
44
metadata:
55
title: 'Exercise: Implement events in C#'
@@ -10,4 +10,4 @@ metadata:
1010
ms.topic: unit
1111
durationInMinutes: 25
1212
content: |
13-
[!include[](includes/6-exercise-events-csharp.md)]
13+
[!include[](includes/6-exercise-implement-events-csharp.md)]

learn-pr/wwl-language/create-manage-events/includes/1-introduction.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Imagine you're a developer working on a desktop application for a retail store.
44

55
## Learning objectives
66

7-
- Understand Events and Delegates in C#
8-
- Examine and Declare Events in C#
9-
- Encapsulate and Raise Events in C#
10-
- Manage Event Subscription and Unsubscription in C#
7+
- Understand Events and Delegates in C#.
8+
- Examine and Declare Events in C#.
9+
- Encapsulate and Raise Events in C#.
10+
- Manage Event Subscription and Unsubscription in C#.
1111

1212
## Prerequisites
1313

learn-pr/wwl-language/create-manage-events/includes/2-examine-events-csharp.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Events let one part of a program inform other parts when something important occ
88

99
The following diagram illustrates the relationship between a publisher and subscribers in an event-driven model:
1010

11-
![Publisher and Subscriber relationship](../media/PublisherSubscriber.png)
11+
:::image type="content" source="../media/publisher-subscriber.png" alt-text="Diagram showing publisher and subscriber relationship.":::
1212

1313
- The **publisher** raises an event when a specific action occurs, such as the selection of a button.
1414
- The **subscribers** handle the event by executing their respective event handler methods.
@@ -51,7 +51,7 @@ Lambda expressions (`=>`) are used in the subscription to define the event handl
5151

5252
Delegates are type-safe function pointers that allow methods to be passed as parameters and invoked dynamically. Events in C# are built on the foundation of delegates, which define the method signature for event handlers.
5353

54-
**How delegates relate to events:**
54+
**How delegates relate to events**:
5555

5656
- Delegates define the method signature that event handlers must match.
5757
- Events use delegates to notify subscribers when something significant occurs.
@@ -83,8 +83,8 @@ The design of events in C# aims to facilitate minimal coupling between component
8383

8484
**Goals of event design**
8585

86-
- **Minimal coupling:** Events enable interaction between an event source and an event sink without requiring tight dependencies. Minimal coupling, using events, is especially useful when components developed by different teams or updated independently.
87-
- **Ease of subscription and unsubscription:** The syntax for subscribing (`+=`) and unsubscribing (`-=`) is straightforward.
88-
- **Support for multiple subscribers:** Events can have zero, one, or multiple subscribers, making them versatile for various scenarios.
86+
- **Minimal coupling**: Events enable interaction between an event source and an event sink without requiring tight dependencies. Minimal coupling, using events, is especially useful when components developed by different teams or updated independently.
87+
- **Ease of subscription and unsubscription**: The syntax for subscribing (`+=`) and unsubscribing (`-=`) is straightforward.
88+
- **Support for multiple subscribers**: Events can have zero, one, or multiple subscribers, making them versatile for various scenarios.
8989

9090
Events are often used in scenarios where user actions or system changes need to be communicated across components. For example, a `Closing` event might allow subscribers to cancel the close operation or modify its behavior.

learn-pr/wwl-language/create-manage-events/includes/3-declare-events-csharp.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ This example demonstrates how a derived class can override the base class's even
8585

8686
## Best practices for declaring events
8787

88-
- **Encapsulation:** Use the `protected` access modifier for event-raising methods to ensure events are raised only within the declaring class or its derived classes.
89-
- **Virtual methods:** Use `protected virtual` methods to allow derived classes to customize event-raising logic.
90-
- **Event naming:** Use meaningful names for events to clearly indicate their purpose.
88+
- **Encapsulation**: Use the `protected` access modifier for event-raising methods to ensure events are raised only within the declaring class or its derived classes.
89+
- **Virtual methods**: Use `protected virtual` methods to allow derived classes to customize event-raising logic.
90+
- **Event naming**: Use meaningful names for events to clearly indicate their purpose.
9191

9292
By understanding how to declare and use events in practical scenarios, you can build flexible and maintainable applications that respond effectively to user actions and system changes.

learn-pr/wwl-language/create-manage-events/includes/4-subscribe-unsubscribe-events-csharp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ In this example, code contains dynamically managed event subscriptions. The `isS
127127

128128
Memory leaks can occur when event handlers aren't unsubscribed, especially if the event publisher outlives the subscriber. To avoid memory leaks, always unsubscribe from events when they're no longer needed.
129129

130-
**Best practices for avoiding memory leaks:**
130+
**Best practices for avoiding memory leaks**:
131131

132132
- Use weak references or `WeakEventManager` in scenarios where the subscriber's lifetime is shorter than the publisher's.
133133
- Unsubscribe from events in the `Dispose` method of disposable objects.

learn-pr/wwl-language/create-manage-events/includes/5-explore-delegates-events-relationship-csharp.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ As you continue developing the retail store application, your team is now focuse
66

77
Delegates are type-safe function pointers that allow methods to be passed as parameters and invoked dynamically. In other words, a delegate acts like a "contract" that specifies the method's signature, allowing you to pass methods around like variables. Events encapsulate delegates, providing a structured way to notify subscribers when something significant occurs. This unit explores how to use built-in delegates like `EventHandler` and `EventHandler<T>` to simplify event handling.
88

9-
### Using `EventHandler<T>` with Custom Event Data
9+
### Using `EventHandler<T>` with custom event data
1010

1111
The `EventHandler<T>` delegate is a built-in delegate that simplifies event handling when custom event data needs to be passed to subscribers. It allows you to define event handlers that include additional information about the event.
1212

@@ -62,7 +62,7 @@ In this example, the `OrderProcessed` event is declared as `EventHandler<OrderEv
6262

6363
Lambda expressions, as shown in the subscription to the `OrderProcessed` event, are a standard way to concisely define event handlers inline.
6464

65-
### Multicast Delegates
65+
### Multicast delegates
6666

6767
Multicast delegates allow multiple methods to be invoked for a single event. Multicast delegates are useful when multiple subscribers need to respond to the same event, such as notifying different systems or components.
6868

@@ -99,18 +99,23 @@ public class Program
9999

100100
In this example, lambda expressions (`=>`) are used to define the event handlers for the `NotificationSent` event. Lambda expressions are frequently encountered with delegates and provide a concise and readable way to define event handlers, especially when the logic for each handler is straightforward.
101101

102-
### Best Practices for Managing Events
102+
### Best practices for managing events
103103

104104
Managing events effectively is critical for building robust and maintainable applications. Here are some best practices to follow:
105105

106-
1. **Avoid Memory Leaks:**
107-
Always unsubscribe from events when they're no longer needed, especially if the event publisher has a longer lifetime than the subscriber.
106+
- **Avoid Memory Leaks**:
108107

109-
2. **Use Weak References:**
110-
In scenarios where the subscriber's lifetime is shorter than the publisher's, consider using weak references or the `WeakEventManager` to avoid memory leaks.
108+
Always unsubscribe from events when they're no longer needed, especially if the event publisher has a longer lifetime than the subscriber.
111109

112-
3. **Ensure Thread Safety:**
113-
If events are raised in a multithreaded environment, ensure thread safety by using synchronization mechanisms like locks or thread-safe collections.
110+
111+
- **Use Weak References**:
112+
113+
In scenarios where the subscriber's lifetime is shorter than the publisher's, consider using weak references or the `WeakEventManager` to avoid memory leaks.
114+
115+
116+
- **Ensure Thread Safety**:
117+
118+
If events are raised in a multithreaded environment, ensure thread safety by using synchronization mechanisms like locks or thread-safe collections.
114119

115120
**Example: Unsubscribing to avoid memory leaks**
116121

@@ -132,7 +137,7 @@ public class Subscriber : IDisposable
132137
}
133138
```
134139

135-
### Summary
140+
## Summary
136141

137142
In this unit, you explored advanced concepts of delegates and events in C#, including:
138143

learn-pr/wwl-language/create-manage-events/includes/8-summary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ After completing this module, you're now able to:
88
- Subscribe to and unsubscribe from events programmatically to manage resources effectively.
99
- Apply modern .NET Core practices for event handling, including the use of `EventHandler` and custom `EventArgs` classes.
1010

11-
Now that you have a basic understanding of events, consider reviewing the following resources.
11+
Now that you have a basic understanding of events, consider reviewing the following resources:
1212

1313
- [Events (C# Programming Guide)](/dotnet/csharp/programming-guide/events/)
1414
- [Delegates and Events in C#](/dotnet/csharp/programming-guide/delegates-and-events/)

learn-pr/wwl-language/create-manage-events/index.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### YamlMime:Module
22
uid: learn.wwl.create-manage-events
33
metadata:
4-
title: Create and manage events
4+
title: Create and Manage Events
55
description: "Discover how events facilitate communication between objects in C#, and how their integration with delegates creates robust, decoupled code."
66
ms.date: 04/23/2025
77
author: wwlpublish
@@ -14,13 +14,13 @@ metadata:
1414
title: Create and manage events
1515
summary: Learn the fundamentals of events in C#, including their reliance on delegates and their role in enabling communication between objects.
1616
abstract: |
17-
- Understand Events and Delegates in C#
17+
- Understand Events and Delegates in C#.
1818
19-
- Examine and Declare Events in C#
19+
- Examine and Declare Events in C#.
2020
21-
- Encapsulate and Raise Events in C#
21+
- Encapsulate and Raise Events in C#.
2222
23-
- Manage Event Subscription and Unsubscription in C#
23+
- Manage Event Subscription and Unsubscription in C#.
2424
prerequisites: |
2525
- An installation of Visual Studio Code with the C# Dev Kit extension installed.
2626
@@ -47,7 +47,7 @@ units:
4747
- learn.wwl.create-manage-events.declare-events-csharp
4848
- learn.wwl.create-manage-events.subscribe-unsubscribe-events-csharp
4949
- learn.wwl.create-manage-events.explore-delegates-events-relationship-csharp
50-
- learn.wwl.create-manage-events.exercise-events-csharp
50+
- learn.wwl.create-manage-events.exercise-implement-events-csharp
5151
- learn.wwl.create-manage-events.knowledge-check
5252
- learn.wwl.create-manage-events.summary
5353
badge:

0 commit comments

Comments
 (0)