Skip to content

Commit 6c5135d

Browse files
committed
Fix the open issues
- Add an example using an anonymouse method - Remove the references to C++ function pointers - Rewrite one heading
1 parent 9c08edd commit 6c5135d

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

docs/csharp/programming-guide/delegates/delegates-with-named-vs-anonymous-methods.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ A [delegate](../../language-reference/builtin-types/reference-types.md) can be a
1313

1414
[!code-csharp[csProgGuideDelegates#1](./snippets/Delegates.cs#1)]
1515

16-
This is called using a named method. Delegates constructed with a named method can encapsulate either a [static](../../language-reference/keywords/static.md) method or an instance method. Named methods are the only way to instantiate a delegate in earlier versions of C#. However, in a situation where creating a new method is unwanted overhead, C# enables you to instantiate a delegate and immediately specify a code block that the delegate will process when it is called. The block can contain either a [lambda expression](../../language-reference/operators/lambda-expressions.md) or an [anonymous method](../../language-reference/operators/delegate-operator.md).
16+
17+
This is called using a named method. Delegates constructed with a named method can encapsulate either a [static](../../language-reference/keywords/static.md) method or an instance method. Named methods are the only way to instantiate a delegate in earlier versions of C#. However, in a situation where creating a new method is unwanted overhead, C# enables you to instantiate a delegate and immediately specify a code block that the delegate will process when it is called. The block can contain either a [lambda expression](../../language-reference/operators/lambda-expressions.md) or an [anonymous method](../../language-reference/operators/delegate-operator.md), as shown in the following example:
18+
19+
:::code language="csharp" source="./snippets/Delegates.cs" id="SnippetAnonymousMethod":::
1720

1821
The method that you pass as a delegate parameter must have the same signature as the delegate declaration. A delegate instance may encapsulate either static or instance method.
1922

docs/csharp/programming-guide/delegates/how-to-declare-instantiate-and-use-a-delegate.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You can declare delegates using any of the following methods:
3939

4040
[!code-csharp[csProgGuideDelegates#12](./snippets/Delegates.cs#12)]
4141

42-
## Robust Programming
42+
## Work with delegates
4343

4444
- Declaring a delegate.
4545

docs/csharp/programming-guide/delegates/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ This ability to refer to a method as a parameter makes delegates ideal for defin
2828

2929
Delegates have the following properties:
3030

31-
- Delegates are similar to C++ function pointers, but delegates are fully object-oriented, and unlike C++ pointers to member functions, delegates encapsulate both an object instance and a method.
3231
- Delegates allow methods to be passed as parameters.
3332
- Delegates can be used to define callback methods.
3433
- Delegates can be chained together; for example, multiple methods can be called on a single event.

docs/csharp/programming-guide/delegates/snippets/Delegates.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ void DoWork(int k) { /* ... */ }
5757
WorkCallback d = obj.DoWork;
5858
//</Snippet1>
5959
}
60+
61+
class Temp2
62+
{
63+
static Temp2 obj = new Temp2();
64+
65+
//<SnippetAnonymousMethod>
66+
// Declare a delegate.
67+
delegate void WorkCallback(int x);
68+
69+
// Instantiate the delegate using an anonymous method.
70+
WorkCallback d = (int k) => { /* ... */ };
71+
//</SnippetAnonymousMethod>
72+
}
6073
}
6174

6275
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)