Skip to content

Commit f6534e9

Browse files
authored
Freshness Edit: dotnet content (#45192)
* edit pass * edits per review feedback from billwagner
1 parent baeb2bc commit f6534e9

File tree

1 file changed

+24
-21
lines changed
  • docs/csharp/programming-guide/delegates

1 file changed

+24
-21
lines changed
Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,55 @@
11
---
2-
title: "Delegates"
3-
description: A delegate in C# is a type that refers to methods with a parameter list and return type. Delegates are used to pass methods as arguments to other methods.
4-
ms.date: 12/20/2024
2+
title: Work with delegate types in C#
3+
description: Explore delegate types in C#. A delegate is a date type that refers to a method with a defined parameter list and return type. You use delegates to pass methods as arguments to other methods.
4+
ms.date: 03/11/2025
55
helpviewer_keywords:
66
- "C# language, delegates"
77
- "delegates [C#]"
88
---
99
# Delegates (C# Programming Guide)
1010

11-
A [delegate](../../language-reference/builtin-types/reference-types.md) is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
11+
A [delegate](../../language-reference/builtin-types/reference-types.md) is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate the delegate instance with any method that has a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
1212

13-
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs. The following example shows a delegate declaration:
13+
Delegates are used to pass methods as arguments to other methods. Event handlers are essentially methods you invoke through delegates. When you create a custom method, a class such as a Windows control can call your method when a certain event occurs.
14+
15+
The following example shows a delegate declaration:
1416

1517
:::code language="csharp" source="./snippets/Overview.cs" id="DelegateDeclaration":::
1618

17-
Any method from any accessible class or struct that matches the delegate type can be assigned to the delegate. The method can be either static or an instance method. This flexibility means you can programmatically change method calls, or plug new code into existing classes.
19+
You can assign any method from any accessible class or struct that matches the delegate type to the delegate. The method can be either static or an instance method. The flexibility allows you to programmatically change method calls, or plug new code into existing classes.
1820

1921
> [!NOTE]
20-
> In the context of method overloading, the signature of a method does not include the return value. But in the context of delegates, the signature does include the return value. In other words, a method must have the same return type as the delegate.
22+
> In the context of method overloading, the signature of a method doesn't include the return value. However, in the context of delegates, the signature does include the return value. In other words, a method must have a compatible return type as the return type declared by the delegate.
2123
22-
This ability to refer to a method as a parameter makes delegates ideal for defining callback methods. You can write a method that compares two objects in your application. That method can be used in a delegate for a sort algorithm. Because the comparison code is separate from the library, the sort method can be more general.
24+
The ability to refer to a method as a parameter makes delegates ideal for defining callback methods. You can write a method that compares two objects in your application. The method can then be used in a delegate for a sort algorithm. Because the comparison code is separate from the library, the sort method can be more general.
2325

24-
[Function pointers](~/_csharplang/proposals/csharp-9.0/function-pointers.md) support similar scenarios, where you need more control over the calling convention. The code associated with a delegate is invoked using a virtual method added to a delegate type. Using function pointers, you can specify different conventions.
26+
[Function pointers](~/_csharplang/proposals/csharp-9.0/function-pointers.md) support similar scenarios, where you need more control over the calling convention. The code associated with a delegate is invoked by using a virtual method added to a delegate type. When you work with function pointers, you can specify different conventions.
2527

26-
## Delegates Overview
28+
## Explore delegate characteristics
2729

28-
Delegates have the following properties:
30+
Delegates have the following characteristics:
2931

3032
- Delegates allow methods to be passed as parameters.
3133
- Delegates can be used to define callback methods.
32-
- Delegates can be chained together; for example, multiple methods can be called on a single event.
34+
- Delegates can be chained together, such as calling multiple methods on a single event.
3335
- Methods don't have to match the delegate type exactly. For more information, see [Using Variance in Delegates](../concepts/covariance-contravariance/using-variance-in-delegates.md).
3436
- Lambda expressions are a more concise way of writing inline code blocks. Lambda expressions (in certain contexts) are compiled to delegate types. For more information about lambda expressions, see [Lambda expressions](../../language-reference/operators/lambda-expressions.md).
3537

36-
## In This Section
38+
## Review related articles
39+
40+
For more information about delegates, see the following articles:
3741

38-
- [Using Delegates](./using-delegates.md)
39-
- [When to Use Delegates Instead of Interfaces (C# Programming Guide)](/previous-versions/visualstudio/visual-studio-2010/ms173173(v=vs.100))
40-
- [Delegates with Named vs. Anonymous Methods](./delegates-with-named-vs-anonymous-methods.md)
41-
- [Using Variance in Delegates](../concepts/covariance-contravariance/using-variance-in-delegates.md)
42-
- [How to combine delegates (Multicast Delegates)](./how-to-combine-delegates-multicast-delegates.md)
42+
- [Using delegates](./using-delegates.md)
43+
- [Delegates with named versus anonymous methods](./delegates-with-named-vs-anonymous-methods.md)
44+
- [Using variance in delegates](../concepts/covariance-contravariance/using-variance-in-delegates.md)
45+
- [How to combine delegates (multicast delegates)](./how-to-combine-delegates-multicast-delegates.md)
4346
- [How to declare, instantiate, and use a delegate](./how-to-declare-instantiate-and-use-a-delegate.md)
4447

45-
## C# Language Specification
48+
## Access the C# language specification
4649

47-
For more information, see [Delegates](~/_csharpstandard/standard/delegates.md) in the [C# Language Specification](~/_csharpstandard/standard/README.md). The language specification is the definitive source for C# syntax and usage.
50+
The language specification is the definitive source for C# syntax and usage. For more information, see [Delegates](~/_csharpstandard/standard/delegates.md) in the [C# Language Specification](~/_csharpstandard/standard/README.md).
4851

49-
## See also
52+
## Related links
5053

5154
- <xref:System.Delegate>
5255
- [Events](../events/index.md)

0 commit comments

Comments
 (0)