What is the relationship between delegates and events?

Delegatedoes the same job of function pointers. It can be considered as pointers to the functions of the controlled world. It simply represents the address of the function being called, as well as the specific object whose method should be called.

Many times I read the term Delegatealong with the term Event, but I do not see the relationship between them. Is it a Eventspecific type of delegate?

+5
source share
5 answers

Short answer: see my article on this topic . Longer answer:

- . / (aka ), .

, - :

public event EventHandler Foo;

:

public void AddFooHandler(EventHandler handler) { ... }
public void RemoveFooHandler(EventHandler handler) { ... }

, , . , ( ), "" .

+15

, "" . -.

"" . , . , , , .

A Button # , . , , .

, - . , " , , , ". , , . , , , , , .

; : " , ". , , .

. , , . - , . . , - , .

+10

, , . , , .

+4

.

( )

- , . , , - . , , . , , .

, () , . ( ) ..NET Framework (), .

- , . , , . , . , . . , . .

:

http://msdn.microsoft.com/en-us/library/17sde2xt(v=vs.100).aspx

+3

A Delegate - , , , ( null), , , . , , . , , , , . , , /, , MulticastDelegate.

An Event - , , , , , , . MulticastDelegate, ( MulticastDelegate ), "" "", ", .

:

  1. , , , (, ).
  2. # vb.net "" "" , "MulticastDelegate" , - . # MulticastDelegate , , , #, `eventName + = someDelegate` , , .
  3. , , , . , / ; , / , , , , /.
  4. `MulticastDelegate`, ` Delegate.Combine`, 'add', , , , , "MulticastDelegate" , , "" .

    № 2 , "" , #, , . "" - ( VB "" , , "" " (.. , " "), , RaiseEvent EventName(params) ).

+3

All Articles