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?
AA class that inherits from another class
BA variable that stores data
CA type of loop
DA reference to a method with a specific signature
✗ Incorrect
A delegate holds references to methods matching its signature, enabling callbacks.
How do delegates support the callback pattern?
ABy creating new classes dynamically
BBy storing data in arrays
CBy allowing methods to be passed as parameters and called later
DBy running code in parallel automatically
✗ Incorrect
Delegates let you pass methods as arguments, which can be called back later.
What keyword is used to declare a delegate in C#?
Afunction
Bdelegate
Ccallback
Devent
✗ Incorrect
The 'delegate' keyword declares a delegate type.
What happens when a multicast delegate is invoked?
AAll methods assigned to it are called in order
BOnly the first method is called
CIt throws an error
DIt returns a list of methods
✗ Incorrect
Multicast delegates call all their assigned methods sequentially.
Which of these is a benefit of using delegates for callbacks?
AThey allow loose coupling between caller and callee
BThey increase program size significantly
CThey prevent any method from being called
DThey make code run slower always
✗ 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.
Practice
(1/5)
1. What is the main purpose of using delegates as a callback pattern in C#?
easy
A. To handle exceptions automatically
B. To create new classes dynamically
C. To pass methods as parameters and execute them later
D. To convert data types implicitly
Solution
Step 1: Understand delegate usage
Delegates allow methods to be passed as parameters to other methods.
Step 2: Identify callback purpose
This passing enables callbacks, which run code after some work is done.
Final Answer:
To pass methods as parameters and execute them later -> Option C
Quick Check:
Delegates = method parameters for callbacks [OK]
Hint: Delegates let you pass methods to run later [OK]
Common Mistakes:
Confusing delegates with classes
Thinking delegates handle exceptions
Assuming delegates convert data types
2. Which of the following is the correct syntax to declare a delegate that takes an int and returns void?
easy
A. MyDelegate delegate void(int number);
B. void delegate MyDelegate(int number);
C. delegate MyDelegate void(int number);
D. delegate void MyDelegate(int number);
Solution
Step 1: Recall delegate declaration syntax
The correct syntax is: delegate returnType DelegateName(parameterList);
A. Starting process...
Hello, World!
Process finished.
B. Hello, World!
Starting process...
Process finished.
C. Starting process...
Process finished.
Hello, World!
D. Process finished.
Starting process...
Hello, World!
Solution
Step 1: Trace method calls in Main and RunProcess
Main creates a callback that prints "Hello, {msg}!" and passes it to RunProcess. RunProcess prints "Starting process...", calls callback with "World", then prints "Process finished.".
Step 2: Determine output order
Output lines are: "Starting process...", "Hello, World!", "Process finished." in that order.
Final Answer:
Starting process...
Hello, World!
Process finished. -> Option A
Quick Check:
Callback runs between start and finish prints [OK]
Hint: Callbacks run where invoked inside method [OK]
Common Mistakes:
Assuming callback runs before 'Starting process...'
Mixing output order
Ignoring lambda execution timing
4. Identify the error in this code snippet using delegates as callbacks:
B. Delegate signature does not match method signature
C. Callback method is not called
D. Missing return statement in NotifyMethod
Solution
Step 1: Compare delegate and method signatures
Delegate Notify expects a method with no parameters and void return. NotifyMethod takes a string parameter, so signatures differ.
Step 2: Understand delegate assignment rules
Assigning NotifyMethod to Notify delegate causes a compile-time error due to signature mismatch.
Final Answer:
Delegate signature does not match method signature -> Option B
Quick Check:
Delegate and method signatures must match [OK]
Hint: Delegate and method signatures must match exactly [OK]
Common Mistakes:
Ignoring parameter mismatch
Thinking delegate instantiation is wrong
Assuming missing return causes error here
5. You want to create a method ProcessData that takes an integer array and a delegate callback to process each element asynchronously. Which approach correctly uses delegates as callbacks to print each element after processing?
hard
A. Define a delegate Action<int> and call it inside a loop with Task.Run(() => callback(item))
B. Use a delegate with no parameters and call it once after processing all elements
C. Pass a delegate that returns int and ignore its return value
D. Use a delegate that takes a string but pass integers directly
Solution
Step 1: Identify delegate type for element processing
Since each element is an int and we want to process it, Action<int> fits as it takes int and returns void.
Step 2: Use asynchronous calls inside loop
Calling Task.Run(() => callback(item)) runs the callback asynchronously for each element.
Final Answer:
Define a delegate Action<int> and call it inside a loop with Task.Run(() => callback(item)) -> Option A
Quick Check:
Delegate matches parameter and async call runs callback per item [OK]
Hint: Use Action<int> and async calls inside loop [OK]
Common Mistakes:
Using delegate with wrong parameters
Calling callback only once instead of per item
Ignoring async execution
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.