Recall & Review
beginner
What is a delegate in C#?
A delegate is a type that holds references to methods with a specific signature. It allows methods to be passed as parameters or assigned to variables.
Click to reveal answer
beginner
How does a delegate enable the callback pattern?
A delegate lets you pass a method as an argument to another method. The receiving method can then call the passed method later, acting as a callback.
Click to reveal answer
intermediate
Show a simple example of a delegate used as a callback.
public delegate void Notify(string message);
public class Process {
public void Start(Notify callback) {
// Do some work
callback("Process finished");
}
}
// Usage
Process p = new Process();
p.Start(msg => Console.WriteLine(msg));Click to reveal answer
intermediate
What is the benefit of using delegates for callbacks?
Delegates provide flexibility by allowing different methods to be called without changing the caller code. This supports loose coupling and reusable code.
Click to reveal answer
advanced
Can delegates point to multiple methods? What is this called?
Yes, delegates can point to multiple methods. This is called a multicast delegate. When invoked, all methods are called in order.
Click to reveal answer
What does a delegate in C# represent?
✗ Incorrect
A delegate holds references to methods matching its signature, enabling callbacks.
How do delegates support the callback pattern?
✗ Incorrect
Delegates let you pass methods as arguments, which can be called back later.
What keyword is used to declare a delegate in C#?
✗ Incorrect
The 'delegate' keyword declares a delegate type.
What happens when a multicast delegate is invoked?
✗ Incorrect
Multicast delegates call all their assigned methods sequentially.
Which of these is a benefit of using delegates for callbacks?
✗ Incorrect
Delegates enable loose coupling by letting different methods be called without changing the caller.
Explain how delegates enable the callback pattern in C#.
Think about how you can give a method to another method to call back.
You got /4 concepts.
Describe the difference between a single-cast and multicast delegate.
Consider how many methods a delegate can point to.
You got /4 concepts.