Skip to content

Commit 7483fb8

Browse files
svickRon Petrusha
authored andcommitted
Synchronize custom event accessor sample with larger sample (#6324)
1 parent 5338b5d commit 7483fb8

File tree

3 files changed

+33
-46
lines changed

3 files changed

+33
-46
lines changed

docs/csharp/programming-guide/events/codesnippet/CSharp/how-to-implement-interface-events_1.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#region everything
12
namespace WrapTwoInterfaceEvents
23
{
34
using System;
@@ -29,6 +30,7 @@ public class Shape : IDrawingObject, IShape
2930
// Explicit interface implementation required.
3031
// Associate IDrawingObject's event with
3132
// PreDrawEvent
33+
#region IDrawingObjectOnDraw
3234
event EventHandler IDrawingObject.OnDraw
3335
{
3436
add
@@ -46,6 +48,8 @@ event EventHandler IDrawingObject.OnDraw
4648
}
4749
}
4850
}
51+
#endregion
52+
4953
// Explicit interface implementation required.
5054
// Associate IShape's event with
5155
// PostDrawEvent
@@ -133,3 +137,4 @@ Sub1 receives the IDrawingObject event.
133137
Drawing a shape.
134138
Sub2 receives the IShape event.
135139
*/
140+
#endregion

docs/csharp/programming-guide/events/how-to-implement-custom-event-accessors.md

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,7 @@ An event is a special kind of multicast delegate that can only be invoked from w
1515
## Example
1616
The following example shows how to implement custom add and remove event accessors. Although you can substitute any code inside the accessors, we recommend that you lock the event before you add or remove a new event handler method.
1717

18-
```csharp
19-
event EventHandler IDrawingObject.OnDraw
20-
{
21-
add
22-
{
23-
lock (PreDrawEvent)
24-
{
25-
PreDrawEvent += value;
26-
}
27-
}
28-
remove
29-
{
30-
lock (PreDrawEvent)
31-
{
32-
PreDrawEvent -= value;
33-
}
34-
}
35-
}
36-
```
18+
[!code-csharp[IDrawingObject.OnDraw](codesnippet/CSharp/how-to-implement-interface-events_1.cs#IDrawingObjectOnDraw)]
3719

3820
## See Also
3921
[Events](../../../csharp/programming-guide/events/index.md)

docs/csharp/programming-guide/events/how-to-implement-interface-events.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,47 +9,47 @@ ms.assetid: 63527447-9535-4880-8e95-35e2075827df
99
# How to: Implement Interface Events (C# Programming Guide)
1010
An [interface](../../../csharp/language-reference/keywords/interface.md) can declare an [event](../../../csharp/language-reference/keywords/event.md). The following example shows how to implement interface events in a class. Basically the rules are the same as when you implement any interface method or property.
1111

12-
### To implement interface events in a class
12+
## To implement interface events in a class
1313

14-
- Declare the event in your class and then invoke it in the appropriate areas.
14+
Declare the event in your class and then invoke it in the appropriate areas.
1515

16-
```csharp
17-
namespace ImplementInterfaceEvents
16+
```csharp
17+
namespace ImplementInterfaceEvents
18+
{
19+
public interface IDrawingObject
1820
{
19-
public interface IDrawingObject
20-
{
21-
event EventHandler ShapeChanged;
22-
}
23-
public class MyEventArgs : EventArgs
21+
event EventHandler ShapeChanged;
22+
}
23+
public class MyEventArgs : EventArgs
24+
{
25+
// class members
26+
}
27+
public class Shape : IDrawingObject
28+
{
29+
public event EventHandler ShapeChanged;
30+
void ChangeShape()
2431
{
25-
// class members
32+
// Do something here before the event…
33+
34+
OnShapeChanged(new MyEventArgs(/*arguments*/));
35+
36+
// or do something here after the event.
2637
}
27-
public class Shape : IDrawingObject
38+
protected virtual void OnShapeChanged(MyEventArgs e)
2839
{
29-
public event EventHandler ShapeChanged;
30-
void ChangeShape()
31-
{
32-
// Do something here before the event…
33-
34-
OnShapeChanged(new MyEventArgs(/*arguments*/));
35-
36-
// or do something here after the event.
37-
}
38-
protected virtual void OnShapeChanged(MyEventArgs e)
39-
{
40-
ShapeChanged?.Invoke(this, e);
41-
}
40+
ShapeChanged?.Invoke(this, e);
4241
}
43-
4442
}
45-
```
43+
44+
}
45+
```
4646

4747
## Example
4848
The following example shows how to handle the less-common situation in which your class inherits from two or more interfaces and each interface has an event with the same name. In this situation, you must provide an explicit interface implementation for at least one of the events. When you write an explicit interface implementation for an event, you must also write the `add` and `remove` event accessors. Normally these are provided by the compiler, but in this case the compiler cannot provide them.
4949

5050
By providing your own accessors, you can specify whether the two events are represented by the same event in your class, or by different events. For example, if the events should be raised at different times according to the interface specifications, you can associate each event with a separate implementation in your class. In the following example, subscribers determine which `OnDraw` event they will receive by casting the shape reference to either an `IShape` or an `IDrawingObject`.
5151

52-
[!code-csharp[csProgGuideEvents#10](../../../csharp/programming-guide/events/codesnippet/CSharp/how-to-implement-interface-events_1.cs)]
52+
[!code-csharp[WrapTwoInterfaceEvents](../../../csharp/programming-guide/events/codesnippet/CSharp/how-to-implement-interface-events_1.cs#everything)]
5353

5454
## See Also
5555
[C# Programming Guide](../../../csharp/programming-guide/index.md)

0 commit comments

Comments
 (0)