📖 Scenario: Imagine you are building a simple notification system where different actions can be triggered when an event happens, like sending an email or logging a message.
🎯 Goal: You will create a delegate to hold references to methods and use it to call different actions dynamically. This shows why delegates are useful for flexible and reusable code.
📋 What You'll Learn
Create a delegate type called Notify that takes a string parameter and returns void
Create two methods called SendEmail and LogMessage that match the delegate signature
Create a variable of type Notify and assign it to SendEmail
Add LogMessage to the delegate invocation list
Invoke the delegate with a message string
Print the output of both methods being called
💡 Why This Matters
🌍 Real World
Delegates are used in event handling, callbacks, and designing flexible APIs where the exact method to run can change.
💼 Career
Understanding delegates is essential for C# developers to write clean, maintainable, and flexible code, especially in GUI apps and asynchronous programming.
Progress0 / 4 steps
1
Create the delegate type and methods
Create a delegate called Notify that takes a string parameter and returns void. Then create two methods called SendEmail and LogMessage that each take a string parameter and print messages starting with "Email sent:" and "Log entry:" respectively.
C Sharp (C#)
Hint
Delegates are like a type that can hold references to methods with a specific signature.
2
Create a delegate variable and assign a method
Inside the Program class, create a Notify variable called notifyHandler and assign it to the SendEmail method.
C Sharp (C#)
Hint
Assign a method name without parentheses to the delegate variable.
3
Add another method to the delegate invocation list
Add the LogMessage method to the notifyHandler delegate using the += operator so both methods will be called.
C Sharp (C#)
Hint
Use += to add methods to the delegate's invocation list.
4
Invoke the delegate and print output
Call the notifyHandler delegate with the message "Hello World" inside Main to see both methods run and print their messages.
C Sharp (C#)
Hint
Call the delegate like a method with the message string.
Practice
(1/5)
1. Why are delegates needed in C# programming?
easy
A. To allow methods to be passed as parameters and called flexibly
B. To store data in a database
C. To create user interfaces
D. To perform arithmetic calculations
Solution
Step 1: Understand the role of delegates
Delegates let you treat methods like variables, so you can pass them around and call them flexibly.
Step 2: Compare with other options
Storing data, creating interfaces, or arithmetic are unrelated to delegates' purpose.
Final Answer:
To allow methods to be passed as parameters and called flexibly -> Option A
Quick Check:
Delegates enable flexible method calls = D [OK]
Hint: Delegates let you pass methods like variables [OK]
Common Mistakes:
Confusing delegates with data storage
Thinking delegates create UI elements
Assuming delegates perform calculations
2. Which of the following is the correct syntax to declare a delegate in C#?
easy
A. void delegate MyDelegate(string message);
B. delegate void MyDelegate(string message);
C. delegate MyDelegate void(string message);
D. void MyDelegate delegate(string message);
Solution
Step 1: Recall delegate declaration syntax
The correct syntax starts with the keyword 'delegate', followed by the return type, then the delegate name and parameters.
Step 2: Check each option
delegate void MyDelegate(string message); matches the correct syntax. Others have incorrect order or keywords.
Final Answer:
delegate void MyDelegate(string message); -> Option B
Quick Check:
Correct delegate syntax = A [OK]
Hint: Delegate syntax starts with 'delegate' keyword [OK]
Common Mistakes:
Placing 'void' before 'delegate'
Mixing order of keywords
Omitting 'delegate' keyword
3. What will be the output of the following C# code?
delegate void Printer(string message);
class Program {
static void PrintHello(string msg) {
Console.WriteLine("Hello " + msg);
}
static void Main() {
Printer p = PrintHello;
p("World");
}
}
medium
A. PrintHello World
B. World Hello
C. Hello World
D. Compilation error
Solution
Step 1: Understand delegate assignment and call
The delegate 'Printer' is assigned to method 'PrintHello', which prints "Hello " plus the message.