Delegates let you store references to methods. This helps you call methods indirectly and pass them around like variables.
Delegate declaration and instantiation in C Sharp (C#)
delegate return_type DelegateName(parameter_list);
DelegateName delegateInstance = new DelegateName(MethodName);The delegate keyword declares a delegate type.
Delegate instances are created by passing a method name matching the delegate signature.
SayHello.delegate void SimpleDelegate(); void SayHello() { Console.WriteLine("Hello!"); } SimpleDelegate d = new SimpleDelegate(SayHello);
Add method.delegate int MathOperation(int x, int y); int Add(int x, int y) { return x + y; } MathOperation op = new MathOperation(Add);
This program declares a delegate GreetDelegate for methods that take a string and return nothing. It creates instances pointing to two different greeting methods and calls them.
using System; class Program { delegate void GreetDelegate(string name); static void GreetMorning(string name) { Console.WriteLine($"Good morning, {name}!"); } static void GreetEvening(string name) { Console.WriteLine($"Good evening, {name}!"); } static void Main() { GreetDelegate greet = new GreetDelegate(GreetMorning); greet("Alice"); greet = new GreetDelegate(GreetEvening); greet("Bob"); } }
Delegate method signatures must match exactly (parameters and return type).
You can use shorthand syntax: GreetDelegate greet = GreetMorning; without new.
Delegates can point to multiple methods using multicast, but that is an advanced topic.
Delegates hold references to methods with a specific signature.
Declare delegates with the delegate keyword and instantiate by passing a method name.
Delegates enable flexible and reusable code by allowing methods to be passed and called indirectly.
Practice
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 BQuick Check:
Delegate = method reference holder [OK]
- Confusing delegates with variables
- Thinking delegates create classes
- Assuming delegates store simple values
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.Final Answer:
delegate void MyDelegate(int number); -> Option DQuick Check:
delegate returnType Name(params) = delegate void MyDelegate(int number); [OK]
- Placing 'void' before 'delegate'
- Swapping delegate name and return type
- Using 'void' as parameter type
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));
}
}
Solution
Step 1: Understand delegate assignment
The delegate 'Operation' matches the method 'Add' signature (two ints, returns int), so 'op' points to 'Add'.Step 2: Evaluate delegate invocation
Calling 'op(3, 4)' calls 'Add(3, 4)', which returns 3 + 4 = 7.Final Answer:
7 -> Option CQuick Check:
Delegate calls Add(3,4) = 7 [OK]
- Thinking delegate concatenates numbers
- Expecting string output
- Assuming compile error due to delegate
delegate void Printer(string message);
class Program {
static void PrintMessage() { Console.WriteLine("Hello"); }
static void Main() {
Printer p = PrintMessage;
p("Hi");
}
}
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 AQuick Check:
Delegate and method signatures must match [OK]
- Ignoring parameter mismatch
- Thinking delegate needs parentheses on assignment
- Assuming delegate must return a value
delegate bool Filter(int value);
Which of the following correctly instantiates the delegate to a method that returns true if the number is even?
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 AQuick Check:
Delegate needs bool return matching even check [OK]
- Returning wrong type (int instead of bool)
- Missing return statement in anonymous method
- Incorrect condition for even check
Ready to prove it?
Pass the test and this pattern moves unknown
