Delegates and Events (IT1811 Handout 1)

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

1/11

flashcard set

Earn XP

Description and Tags

Flashcards covering core concepts of delegates, generic delegates, events, and the difference between delegates and interfaces as presented in the IT1811 handout. Each card poses a question about a concept and provides the concise definition or explanation.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

12 Terms

1
New cards

What is a delegate in .NET?

A class that encapsulates a method; a reference type that holds a reference to a method and can call any method whose signature matches.

2
New cards

How do you declare a delegate in C#?

Use the delegate keyword with an access modifier, return type, name, and parameters: delegate (). Example: public delegate int mathOp(int x, int y);

3
New cards

How do you instantiate a delegate?

Using the new keyword with a method reference, or by assigning a method group

4
New cards

How do you invoke a delegate?

Call it like a method (e.g., delegateAddition(10, 20)); If it has a return value, use the returned result.

5
New cards

What is a generic delegate?

A delegate not bound to a specific type and can reference methods with parameters and return types of a generic type X (e.g., public delegate X DisplayOutput(X arg);)

6
New cards

What is a key difference between delegates and interfaces?

Delegates are declarations used for callbacks with no implementation; interfaces declare methods that a class must implement.

7
New cards

What is an event in C#?

A member of a class that fires when a specific action occurs; events are linked to delegates and invoke registered event handlers when raised.

8
New cards

Why must a delegate be declared before declaring an event?

Because an event is declared with a delegate type; the event signature must match a delegate.

9
New cards

What is the syntax to declare an event?

public event ;

10
New cards

How do you subscribe to an event?

Using the += operator: event_name += delegate_instance;

11
New cards

What are event accessors and what do they do?

The add and remove accessors; the compiler generates them when you subscribe; they handle registering and unregistering event handlers and can include custom logic.

12
New cards

What does the example MessageBox.Show(delegateAddition(10,20).ToString()) demonstrate?

Displaying the result of invoking a delegate by converting it to a string for output.