A delegate is a type that represents references to methods with a specific parameter list and return type. It allows methods to be passed as parameters or assigned to variables.
Click to reveal answer
beginner
How do you declare a delegate that takes an int and returns void?
You declare it like this:
public delegate void MyDelegate(int number);
Click to reveal answer
beginner
How do you instantiate a delegate to point to a method named 'PrintNumber'?
You instantiate it like this:
MyDelegate d = new MyDelegate(PrintNumber);
or simply
MyDelegate d = PrintNumber;
Click to reveal answer
intermediate
Can a delegate hold references to multiple methods?
Yes, delegates can hold references to multiple methods. This is called a multicast delegate, and it calls all methods in its invocation list when invoked.
Click to reveal answer
beginner
What happens when you invoke a delegate?
When you invoke a delegate, it calls the method(s) it references with the provided arguments, and returns the result if the method has a return type.
Click to reveal answer
How do you declare a delegate that returns an int and takes no parameters?
Adelegate void MyDelegate(int);
Bpublic int delegate MyDelegate();
Cpublic delegate int MyDelegate();
Ddelegate MyDelegate int();
✗ Incorrect
The correct syntax is 'public delegate int MyDelegate();' which declares a delegate returning int and taking no parameters.
Which of the following is a valid way to instantiate a delegate named 'MyDelegate' pointing to method 'Show'?
AMyDelegate d = new MyDelegate(Show);
BMyDelegate d = Show();
CMyDelegate d = delegate Show();
DMyDelegate d = new Show();
✗ Incorrect
You instantiate a delegate by using 'new MyDelegate(MethodName);' or simply 'MyDelegate d = MethodName;'.
What is a multicast delegate?
AA delegate that can reference multiple methods.
BA delegate that returns multiple values.
CA delegate that can only reference one method.
DA delegate that is declared inside a class.
✗ Incorrect
A multicast delegate holds references to multiple methods and calls them all when invoked.
What keyword is used to declare a delegate in C#?
Aaction
Bevent
Cfunc
Ddelegate
✗ Incorrect
The 'delegate' keyword is used to declare a delegate type.
If a delegate references multiple methods, what happens when you invoke it?
AOnly the last method is called.
BAll referenced methods are called in order.
COnly the first method is called.
DAn error occurs.
✗ Incorrect
A multicast delegate calls all methods in its invocation list in order when invoked.
Explain how to declare and instantiate a delegate in C# with an example.
Think about how you define a delegate type and then create an object of that type pointing to a method.
You got /4 concepts.
Describe what happens when you invoke a multicast delegate.
Consider how a delegate can hold more than one method and what that means when you call it.
You got /4 concepts.
Practice
(1/5)
1. What is a delegate in C# used for?
easy
A. To define variables that cannot change
B. To hold references to methods with a specific signature
C. To store values like integers or strings
D. To create new classes dynamically
Solution
Step 1: Understand delegate purpose
A delegate is a type that holds references to methods with a matching signature.
Step 2: Compare options to definition
Only To hold references to methods with a specific signature correctly describes this purpose; others describe unrelated concepts.
Final Answer:
To hold references to methods with a specific signature -> Option B
Quick Check:
Delegate = method reference holder [OK]
Hint: Delegates point to methods, not data or classes [OK]
Common Mistakes:
Confusing delegates with variables
Thinking delegates create classes
Assuming delegates store simple values
2. Which of the following is the correct way to declare a delegate that takes an int and returns void?
easy
A. void delegate MyDelegate(int number);
B. delegate int MyDelegate(void 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);
Step 2: Match syntax to options
delegate void MyDelegate(int number); matches this exactly: delegate void MyDelegate(int number); others have incorrect order or types.
Hint: Remember: 'delegate' keyword + return type + name + parameters [OK]
Common Mistakes:
Placing 'void' before 'delegate'
Swapping delegate name and return type
Using 'void' as parameter type
3. What will be the output of the following code?
delegate int Operation(int x, int y); class Program { static int Add(int a, int b) => a + b; static void Main() { Operation op = Add; Console.WriteLine(op(3, 4)); } }
medium
A. 0
B. 34
C. 7
D. Compile error
Solution
Step 1: Understand delegate assignment
The delegate 'Operation' matches the method 'Add' signature (two ints, returns int), so 'op' points to 'Add'.
Hint: Delegate calls assigned method with given arguments [OK]
Common Mistakes:
Thinking delegate concatenates numbers
Expecting string output
Assuming compile error due to delegate
4. Identify the error in the following delegate instantiation:
delegate void Printer(string message); class Program { static void PrintMessage() { Console.WriteLine("Hello"); } static void Main() { Printer p = PrintMessage; p("Hi"); } }
medium
A. PrintMessage method signature does not match delegate
B. Delegate cannot be assigned to a method
C. Missing parentheses when assigning delegate
D. Delegate must return a value
Solution
Step 1: Compare delegate and method signatures
The delegate 'Printer' expects a method with a string parameter and void return type.
Step 2: Check method signature
'PrintMessage' has no parameters, so it does not match the delegate signature.
Final Answer:
PrintMessage method signature does not match delegate -> Option A
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 needs parentheses on assignment
Assuming delegate must return a value
5. Given the delegate declaration:
delegate bool Filter(int value);
Which of the following correctly instantiates the delegate to a method that returns true if the number is even?
hard
A. Filter f = x => x % 2 == 0;
B. Filter f = (int x) => { return x * 2; };
C. Filter f = delegate(int x) { Console.WriteLine(x); };
D. Filter f = x => x + 2 == 0;
Solution
Step 1: Understand delegate signature
'Filter' expects a method taking an int and returning a bool.
Step 2: Check each option's return type and logic
Filter f = x => x % 2 == 0; returns true if x is even (x % 2 == 0), matching bool return. Filter f = (int x) => { return x * 2; }; returns int, not bool. Filter f = delegate(int x) { Console.WriteLine(x); }; returns void (no return), invalid. Filter f = x => x + 2 == 0; returns bool but checks x + 2 == 0, which is not the even check.
Final Answer:
Filter f = x => x % 2 == 0; -> Option A
Quick Check:
Delegate needs bool return matching even check [OK]
Hint: Match delegate return type and logic exactly [OK]
Common Mistakes:
Returning wrong type (int instead of bool)
Missing return statement in anonymous method
Incorrect condition for even check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.