1/13
Flashcards covering C# delegates, generic delegates, comparison with interfaces, and event handling concepts based on IT1811 Handout 1.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is a delegate in .NET?
A delegate is a class in .NET that encapsulates a method and serves as a reference type data type that holds a reference to a method.
What is the general syntax for declaring a delegate in C#?
What requirement must be met for a delegate to reference a method?
The delegate's signature (return type and parameter count/types) must match the signature of the referenced method.
What are two ways to instantiate a delegate in C#?
By using the new keyword with a method reference (e.g., GetAnswer mdAdd = new GetAnswer(Formula.getSum);) or by direct method assignment (e.g., GetAnswer mdAdd = Formula.Addition;).
What is a generic delegate type?
A delegate that is not bound to any specific data type and can reference a method that returns and takes parameters of different types.
What example code is given in the handout for declaring a generic delegate?
public delegate X DisplayOutput
How do delegates and interfaces compare regarding method implementation capacity?
Delegates can implement a method only once and are used when a class needs more than one implementation, whereas a class implementing an interface can implement all methods associated with it.
How do delegates and interfaces compare in terms of execution and retrieval speed?
Delegates are faster to execute but slower to get, whereas interfaces are slower in executing but faster to get.
What is an event in C#?
An event is a member of a class that acts as a task initiator and is fired whenever a specific action takes place.
What are event handlers?
Event handlers are the methods that are automatically invoked when an event occurs.
What must be declared prior to declaring an event?
A delegate to which the event will be associated must be declared first.
What is the syntax for declaring an event in C#?
public event delegate_name event_name;
What operator and syntax are used to add a delegate to an event?
The += operator is used with the syntax: event_name += delegate_instance;
What are event accessors and what functions do they perform?
Event accessors are the add and remove methods generated by the compiler; add registers a new subscription to an event, while remove unregisters a subscription.