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
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/events/how-to-implement-custom-event-accessors.md
+1-19Lines changed: 1 addition & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,25 +15,7 @@ An event is a special kind of multicast delegate that can only be invoked from w
15
15
## Example
16
16
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.
# How to: Implement Interface Events (C# Programming Guide)
10
10
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.
11
11
12
-
###To implement interface events in a class
12
+
## To implement interface events in a class
13
13
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.
15
15
16
-
```csharp
17
-
namespaceImplementInterfaceEvents
16
+
```csharp
17
+
namespaceImplementInterfaceEvents
18
+
{
19
+
publicinterfaceIDrawingObject
18
20
{
19
-
publicinterfaceIDrawingObject
20
-
{
21
-
eventEventHandlerShapeChanged;
22
-
}
23
-
publicclassMyEventArgs : EventArgs
21
+
eventEventHandlerShapeChanged;
22
+
}
23
+
publicclassMyEventArgs : EventArgs
24
+
{
25
+
// class members
26
+
}
27
+
publicclassShape : IDrawingObject
28
+
{
29
+
publiceventEventHandlerShapeChanged;
30
+
voidChangeShape()
24
31
{
25
-
// class members
32
+
// Do something here before the event…
33
+
34
+
OnShapeChanged(newMyEventArgs(/*arguments*/));
35
+
36
+
// or do something here after the event.
26
37
}
27
-
publicclassShape : IDrawingObject
38
+
protectedvirtualvoidOnShapeChanged(MyEventArgse)
28
39
{
29
-
publiceventEventHandlerShapeChanged;
30
-
voidChangeShape()
31
-
{
32
-
// Do something here before the event…
33
-
34
-
OnShapeChanged(newMyEventArgs(/*arguments*/));
35
-
36
-
// or do something here after the event.
37
-
}
38
-
protectedvirtualvoidOnShapeChanged(MyEventArgse)
39
-
{
40
-
ShapeChanged?.Invoke(this, e);
41
-
}
40
+
ShapeChanged?.Invoke(this, e);
42
41
}
43
-
44
42
}
45
-
```
43
+
44
+
}
45
+
```
46
46
47
47
## Example
48
48
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.
49
49
50
50
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`.
0 commit comments