|
1 | 1 | ---
|
2 | 2 | title: "How to declare, instantiate, and use a delegate"
|
3 |
| -description: Learn how to declare, instantiate, and use a delegate. See examples that cover C# 1.0, 2.0, and 3.0 and later. |
| 3 | +description: Learn how to declare, instantiate, and use a delegate. This article provides several examples of declaring, instantiating, and invoking delegates. |
4 | 4 | ms.topic: how-to
|
5 |
| -ms.date: 07/20/2015 |
| 5 | +ms.date: 12/20/2024 |
6 | 6 | helpviewer_keywords:
|
7 | 7 | - "delegates [C#], declaring and instantiating"
|
8 |
| -ms.assetid: 61c4895f-f785-48f8-8bfe-db73b411c4ae |
9 | 8 | ---
|
10 | 9 | # How to declare, instantiate, and use a Delegate (C# Programming Guide)
|
11 | 10 |
|
12 | 11 | You can declare delegates using any of the following methods:
|
13 | 12 |
|
14 | 13 | - Declare a delegate type and declare a method with a matching signature:
|
15 |
| - |
16 |
| - [!code-csharp[csProgGuideDelegates#13](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#13)] |
17 |
| - |
18 |
| - [!code-csharp[csProgGuideDelegates#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#14)] |
19 |
| - |
| 14 | + |
| 15 | + :::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="DeclareNamedDelegate"::: |
| 16 | + |
| 17 | + :::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="CreateNamedInstance"::: |
| 18 | + |
20 | 19 | - Assign a method group to a delegate type:
|
21 |
| - |
22 |
| - [!code-csharp[csProgGuideDelegates#32](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#32)] |
23 |
| - |
24 |
| -- Declare an anonymous method: |
25 |
| - |
26 |
| - [!code-csharp[csProgGuideDelegates#15](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#15)] |
27 |
| - |
| 20 | + |
| 21 | + :::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="MethodGroup"::: |
| 22 | + |
| 23 | +- Declare an anonymous method |
| 24 | + |
| 25 | + :::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="AnonymousMethod"::: |
| 26 | + |
28 | 27 | - Use a lambda expression:
|
29 |
| - |
30 |
| - [!code-csharp[csProgGuideDelegates#31](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#31)] |
31 |
| - |
32 |
| - For more information, see [Lambda Expressions](../../language-reference/operators/lambda-expressions.md). |
33 |
| - |
34 |
| - The following example illustrates declaring, instantiating, and using a delegate. The `BookDB` class encapsulates a bookstore database that maintains a database of books. It exposes a method, `ProcessPaperbackBooks`, which finds all paperback books in the database and calls a delegate for each one. The `delegate` type that is used is named `ProcessBookCallback`. The `Test` class uses this class to print the titles and average price of the paperback books. |
35 |
| - |
36 |
| - The use of delegates promotes good separation of functionality between the bookstore database and the client code. The client code has no knowledge of how the books are stored or how the bookstore code finds paperback books. The bookstore code has no knowledge of what processing is performed on the paperback books after it finds them. |
37 |
| - |
38 |
| -## Example |
39 |
| - |
40 |
| - [!code-csharp[csProgGuideDelegates#12](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#12)] |
41 |
| - |
42 |
| -## Robust Programming |
43 |
| - |
44 |
| -- Declaring a delegate. |
45 |
| - |
46 |
| - The following statement declares a new delegate type. |
47 |
| - |
48 |
| - [!code-csharp[csProgGuideDelegates#16](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#16)] |
49 |
| - |
50 |
| - Each delegate type describes the number and types of the arguments, and the type of the return value of methods that it can encapsulate. Whenever a new set of argument types or return value type is needed, a new delegate type must be declared. |
51 |
| - |
52 |
| -- Instantiating a delegate. |
53 |
| - |
54 |
| - After a delegate type has been declared, a delegate object must be created and associated with a particular method. In the previous example, you do this by passing the `PrintTitle` method to the `ProcessPaperbackBooks` method as in the following example: |
55 |
| - |
56 |
| - [!code-csharp[csProgGuideDelegates#17](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#17)] |
57 |
| - |
58 |
| - This creates a new delegate object associated with the [static](../../language-reference/keywords/static.md) method `Test.PrintTitle`. Similarly, the non-static method `AddBookToTotal` on the object `totaller` is passed as in the following example: |
59 |
| - |
60 |
| - [!code-csharp[csProgGuideDelegates#18](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#18)] |
61 |
| - |
62 |
| - In both cases a new delegate object is passed to the `ProcessPaperbackBooks` method. |
63 |
| - |
64 |
| - After a delegate is created, the method it is associated with never changes; delegate objects are immutable. |
65 |
| - |
66 |
| -- Calling a delegate. |
67 |
| - |
68 |
| - After a delegate object is created, the delegate object is typically passed to other code that will call the delegate. A delegate object is called by using the name of the delegate object, followed by the parenthesized arguments to be passed to the delegate. Following is an example of a delegate call: |
69 |
| - |
70 |
| - [!code-csharp[csProgGuideDelegates#19](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csProgGuideDelegates/CS/Delegates.cs#19)] |
71 |
| - |
72 |
| - A delegate can be either called synchronously, as in this example, or asynchronously by using `BeginInvoke` and `EndInvoke` methods. |
73 |
| - |
| 28 | + |
| 29 | + :::code language="csharp" source="./snippets/HowToDeclareAndUse.cs" id="LambdaExpression"::: |
| 30 | + |
| 31 | +For more information, see [Lambda Expressions](../../language-reference/operators/lambda-expressions.md). |
| 32 | + |
| 33 | +The following example illustrates declaring, instantiating, and using a delegate. The `BookDB` class encapsulates a bookstore database that maintains a database of books. It exposes a method, `ProcessPaperbackBooks`, which finds all paperback books in the database and calls a delegate for each one. The `delegate` type is named `ProcessBookCallback`. The `Test` class uses this class to print the titles and average price of the paperback books. |
| 34 | + |
| 35 | +The use of delegates promotes good separation of functionality between the bookstore database and the client code. The client code has no knowledge of how the books are stored or how the bookstore code finds paperback books. The bookstore code has no knowledge of what processing is performed on the paperback books after it finds them. |
| 36 | + |
| 37 | +:::code language="csharp" source="./snippets/BookStore.cs"::: |
| 38 | + |
74 | 39 | ## See also
|
75 | 40 |
|
76 | 41 | - [Events](../events/index.md)
|
|
0 commit comments