Delegates and Events

0.0(0)
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/21

flashcard set

Earn XP

Description and Tags

Handout 01

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

22 Terms

1

Delegate

Is a class in .NET that encapsulates a method.

It is also a reference type data type that holds a reference method.

This class can call any method as long as its signature matches.

2

delegate keyword

Is used in declaring a delegate on a class.

3

signature methods

Specify what kind of delegate to be declared

4

Read-Only-Text

Read-Only-Text:

To declare a delegate, identify what access modifier should be used first. Then, call the delegate keyword, the return type, the desired name of the delegate, and the parameters inside the open and close parentheses ().

public delegate int mathOp(int x, int y);

5

new keyword

It is used to instantiate the delegate that is associated with the method and supposed to refer on the object.

6

Read-Only-Text

Read-Only-Text: Use new keyword to instantiate the delegate.

GetAnswer mdAdd = new GetAnswer(Formula.getSum);

GetAnswer mdAdd = Formula.Addition;

7

double

What return type is declared in the given example?​

public delegate double Print();

8

public

What access modifier is declared in the givenexample?​

public delegate double BasicMath (double a, double b);

9

Print

What delegate name is declared in the given example?​

public delegate void Print (int value);

10

getName

What is the target method’s name?​

Print p1 = new Print(getName);

11

addition

What is the delegate object’s name?

Print addition = new Print(getSum);

12

Print

What is the declared delegate name?​

Print p1= new Print(getSum);

13

Generic delegates

Are not bound to any specific type. These can reference a method that returns and takes parameters of different types.

14

Read-Only-Text

Read-Only-Text:

declaring a genericdelegate:

public delegate X DisplayOutput(X arg);

15

X

A generic type parameter (can be int, string, etc.)​

16

Event

Is a member of a class that is fired whenever a specific action takes place.

17

Read-Only-Text

Read-Only-Text:

When an event occurs, the method that has been created and registered to this event is automatically invoked.

18

Event handlers

The methods that are invoked when an event occurs

19

+=

This operator allows adding an event to the class.

20

Read-Only-Text

Read-Only-Text: To declare an event

accessModifier event delegateName eventName;

21

Read-Only-Text

Read-Only-Text: To register an event handler.

eventName += new DelegateName (evHandler)

22

Read-Only-Text

Read-Only-Text: To invoke an event: ​

eventName();

or

eventName?.Invoke();