Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 17 Classes

1Since an event accessor implicitly has a parameter named value, it is a compile-time error for a local

2variable or constant declared in an event accessor to have that name.

3[Example: In the following code

4class Control: Component

5{

6

// Unique keys for events

7

static readonly object mouseDownEventKey = new object();

8

static readonly object mouseUpEventKey = new object();

9

// Return event handler associated with key

10

protected Delegate GetEventHandler(object key) {…}

11

// Add event handler associated with key

12

protected void AddEventHandler(object key, Delegate handler) {…}

13

// Remove event handler associated with key

14

protected void RemoveEventHandler(object key, Delegate handler) {…}

15

// MouseDown event

16

public event MouseEventHandler MouseDown {

17

add { AddEventHandler(mouseDownEventKey, value); }

18

remove { RemoveEventHandler(mouseDownEventKey, value); }

19

}

20

// MouseUp event

21

public event MouseEventHandler MouseUp {

22

add { AddEventHandler(mouseUpEventKey, value); }

23

remove { RemoveEventHandler(mouseUpEventKey, value); }

24

}

25

// Invoke the MouseUp event

26

protected void OnMouseUp(MouseEventArgs args) {

27

MouseEventHandler handler;

28

handler = (MouseEventHandler)GetEventHandler(mouseUpEventKey);

29

if (handler != null)

30

handler(this, args);

31}

32}

33the Control class implements an internal storage mechanism for events. The AddEventHandler method

34associates a delegate value with a key, the GetEventHandler method returns the delegate currently

35associated with a key, and the RemoveEventHandler method removes a delegate as an event handler for

36the specified event. Presumably, the underlying storage mechanism is designed such that there is no cost for

37associating a null delegate value with a key, and thus unhandled events consume no storage. end example]

3817.7.3 Static and instance events

39When an event declaration includes a static modifier, the event is said to be a static event. When no

40static modifier is present, the event is said to be an instance event.

41A static event is not associated with a specific instance, and it is a compile-time error to refer to this in the

42accessors of a static event.

43An instance event is associated with a given instance of a class, and this instance can be accessed as this

44(§14.5.7) in the accessors of that event.

45When an event is referenced in a member-access (§14.5.4) of the form E.M, if M is a static event, E shall

46denote a type, and if M is an instance event, E shall denote an instance.

47The differences between static and instance members are discussed further in §17.2.5.

4817.7.4 Virtual, sealed, override, and abstract accessors

49A virtual event declaration specifies that the accessors of that event are virtual. The virtual modifier

50applies to both accessors of an event.

303

Соседние файлы в предмете Электротехника