C# Delegate Operator with Example β A Fun and Easy Guide
Ever felt like your code is getting messy with too many methods? Or maybe you wish you could pass a method like a variable? Well, my friend, say hello to the Delegate Operator in C#! π
By the end of this lesson, youβll understand delegates, write cleaner code, and see a real-world example to make things super clear. Letβs go! π
What is a Delegate?
A delegate is like a function pointer. Think of it as a variable that can hold a method. Instead of calling a method directly, you can store it in a delegate and call it whenever needed.
Hereβs a simple example:
using System;
class Program
{
delegate void Greet(string name); // Declaring a delegate
static void SayHello(string name)
{
Console.WriteLine($"Hello, {name}! π");
}
static void Main()
{
Greet greetDelegate = SayHello; // Assign method to delegate
greetDelegate("Steven"); // Call method using delegate
}
}
Output:
Hello, Steven! π
Breaking Down the Code
delegate void Greet(string name);
β Declares a delegate that holds a method with astring
parameter andvoid
return.static void SayHello(string name)
β A simple method to print a greeting.Greet greetDelegate = SayHello;
β AssignsSayHello
method to the delegate.greetDelegate("Steven");
β Calls the method using the delegate.
See? Instead of calling SayHello
directly, we stored it in a delegate and called it dynamically! Super cool, right? π
Why Use Delegates?
βοΈ Flexibility β Pass methods as parameters.
βοΈ Cleaner Code β Reduce repetitive method calls.
βοΈ Event Handling β Used in event-driven programming.
βοΈ Reusability β Makes code modular and easy to maintain.
Real-World Example β Sending Notifications
Imagine youβre building a notification system. You want the flexibility to send email, SMS, or push notifications based on user choice. Instead of writing separate calls for each, letβs use a delegate!
Hereβs the code:
using System;
class Program
{
// Declare delegate
delegate void NotifyUser(string message);
static void SendEmail(string message)
{
Console.WriteLine($"π§ Email sent: {message}");
}
static void SendSMS(string message)
{
Console.WriteLine($"π± SMS sent: {message}");
}
static void Main()
{
NotifyUser notify;
// User chooses email
notify = SendEmail;
notify("Your order has been shipped!");
// User chooses SMS
notify = SendSMS;
notify("Your OTP is 123456.");
}
}
Output:
π§ Email sent: Your order has been shipped!
π± SMS sent: Your OTP is 123456.
Code Explanation
delegate void NotifyUser(string message);
β Creates a delegate that holds a method with astring
parameter.notify = SendEmail;
β AssignsSendEmail
method tonotify
.notify("Your order has been shipped!");
β CallsSendEmail
.notify = SendSMS;
β Switches toSendSMS
.notify("Your OTP is 123456.");
β CallsSendSMS
.
Boom! π₯ No need for multiple if-else
conditions! Just assign a different method to the delegate and call it. Simple and powerful! π
Wrapping It Up
Today, we explored C# Delegate Operator with example and saw how it makes code more flexible and reusable. We also built a real-world notification system using delegates.
So, what do you think? π€ Have you tried using delegates before? If not, nowβs the time! If you get stuck, donβt panic. Just ask, and Iβll help! π
Next What?
Awesome job! π You just unlocked the power of delegates. Whatβs next? In the next lesson, youβll learn about the Await Operator β a game-changer for handling asynchronous tasks.
Stay curious, my friend! π See you in the next lesson! π