You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Change heading to sentence case
Matches Microsoft style guide and other articles in this section.
* Make all headings sentence case
Co-authored-by: Mark Wiemer (MSFT) <80539004+mwiemer-microsoft@users.noreply.github.com>
* Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
---------
Co-authored-by: Bill Wagner <wiwagn@microsoft.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/csharp/distinguish-delegates-events.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ description: Learn the difference between delegates and events and when to use e
4
4
ms.date: 03/11/2025
5
5
ms.subservice: fundamentals
6
6
---
7
-
# Distinguishing Delegates and Events
7
+
# Delegates vs. events
8
8
9
9
[Previous](modern-events.md)
10
10
@@ -14,30 +14,30 @@ Both offer a late binding scenario: they enable scenarios where a component comm
14
14
15
15
With all those similarities, it's easy to have trouble determining when to use which.
16
16
17
-
## Listening to Events is Optional
17
+
## Listening to events is optional
18
18
19
19
The most important consideration in determining which language feature to use is whether or not there must be an attached subscriber. If your code must call the code supplied by the subscriber, you should use a design based on delegates when you need to implement callback. If your code can complete all its work without calling any subscribers, you should use a design based on events.
20
20
21
21
Consider the examples built during this section. The code you built using `List.Sort()` must be given a comparer function in order to properly sort the elements. LINQ queries must be supplied with delegates in order to determine what elements to return. Both used a design built with delegates.
22
22
23
23
Consider the `Progress` event. It reports progress on a task. The task continues to proceed whether or not there are any listeners. The `FileSearcher` is another example. It would still search and find all the files that were sought, even with no event subscribers attached. UX controls still work correctly, even when there are no subscribers listening to the events. They both use designs based on events.
24
24
25
-
## Return Values Require Delegates
25
+
## Return values require delegates
26
26
27
27
Another consideration is the method prototype you would want for your delegate method. As you saw, the delegates used for events all have a void return type. There are idioms to create event handlers that do pass information back to event sources through modifying properties of the event argument object. While these idioms do work, they aren't as natural as returning a value from a method.
28
28
29
29
Notice that these two heuristics can often both be present: If your delegate method returns a value, it affects the algorithm in some way.
30
30
31
-
## Events Have Private Invocation
31
+
## Events have private invocation
32
32
33
33
Classes other than the one in which an event is contained can only add and remove event listeners; only the class containing the event can invoke the event. Events are typically public class members. By comparison, delegates are often passed as parameters and stored as private class members, if they're stored at all.
34
34
35
-
## Event Listeners Often Have Longer Lifetimes
35
+
## Event listeners often have longer lifetimes
36
36
37
37
The longer lifetime of event listeners is a slightly weaker justification. However, you might find that event-based designs are more natural when the event source is raising events over a long period of time. You can see examples of event-based design for UX controls on many systems. Once you subscribe to an event, the event source can raise events throughout the lifetime of the program. (You can unsubscribe from events when you no longer need them.)
38
38
39
39
Contrast that with many delegate-based designs, where a delegate is used as an argument to a method, and the delegate isn't used after that method returns.
40
40
41
-
## Evaluate Carefully
41
+
## Evaluate carefully
42
42
43
43
The above considerations aren't hard and fast rules. Instead, they represent guidance that can help you decide which choice is best for your particular usage. Because they're similar, you can even prototype both, and consider which would be more natural to work with. They both handle late binding scenarios well. Use the one that communicates your design the best.
0 commit comments