Introduction
Delegates and events are powerful features in C# that allow for flexible and decoupled code design. They are essential for implementing callbacks, event-driven programming, and observer patterns. In this post, we will dive deep into delegates and events, exploring their syntax, usage, and practical examples to help you master these advanced C# topics.
Understanding Delegates in C#
Delegates are type-safe function pointers that allow methods to be passed as parameters. They are used to define callback methods and implement event handling.
Delegate Declaration and Usage Example:
using System;
public delegate void Notify(); // Delegate declaration
public class Process
{
public Notify ProcessCompleted; // Delegate instance
public void StartProcess()
{
Console.WriteLine("Process started...");
// Simulate some work
System.Threading.Thread.Sleep(3000);
// Invoke the delegate when the process is complete
ProcessCompleted?.Invoke();
}
}
public class Program
{
public static void Main(string[] args)
{
Process process = new Process();
process.ProcessCompleted = new Notify(ProcessCompletedHandler); // Delegate assignment
process.StartProcess();
}
public static void ProcessCompletedHandler()
{
Console.WriteLine("Process completed!");
}
}
Explanation:
- Notify Delegate: Declares a delegate named
Notifythat can point to methods with no parameters and no return value. - Process Class: Contains a
StartProcessmethod and a delegate instanceProcessCompleted. - StartProcess Method: Simulates work and invokes the delegate when completed.
- Main Method: Assigns the
ProcessCompletedHandlermethod to theProcessCompleteddelegate and starts the process. - ProcessCompletedHandler Method: Method called when the process is completed.
Output:
Process started... Process completed!
Events in C#: Building Event-Driven Applications
Events are built on top of delegates and provide a way to notify subscribers when something of interest occurs. They are essential for building event-driven applications.
Event Declaration and Usage Example:
using System;
public class Process
{
public event EventHandler ProcessCompleted; // Event declaration
public void StartProcess()
{
Console.WriteLine("Process started...");
// Simulate some work
System.Threading.Thread.Sleep(3000);
OnProcessCompleted(EventArgs.Empty); // Raise the event
}
protected virtual void OnProcessCompleted(EventArgs e)
{
ProcessCompleted?.Invoke(this, e); // Invoke the event
}
}
public class Program
{
public static void Main(string[] args)
{
Process process = new Process();
process.ProcessCompleted += ProcessCompletedHandler; // Event subscription
process.StartProcess();
}
private static void ProcessCompletedHandler(object sender, EventArgs e)
{
Console.WriteLine("Process completed!");
}
}
Explanation:
- Event Declaration: Declares an event
ProcessCompletedusing theEventHandlerdelegate. - OnProcessCompleted Method: Protected method to raise the
ProcessCompletedevent. - Event Subscription: Subscribes the
ProcessCompletedHandlermethod to theProcessCompletedevent. - ProcessCompletedHandler Method: Event handler method that executes when the event is raised.
Output:
Process started... Process completed!
Multicast Delegates
Delegates in C# can point to more than one method, known as multicast delegates.
Example:
using System;
public delegate void Notify(); // Delegate declaration
public class Program
{
public static void Main(string[] args)
{
Notify notify = Method1;
notify += Method2; // Adding another method
notify.Invoke();
}
public static void Method1()
{
Console.WriteLine("Method1 called.");
}
public static void Method2()
{
Console.WriteLine("Method2 called.");
}
}
Explanation:
- Notify Delegate: A delegate that can point to multiple methods.
- Main Method: Assigns
Method1andMethod2to thenotifydelegate. - Invoke Method: Invokes both
Method1andMethod2.
Output:
Method1 called. Method2 called.
Lambda Expressions with Delegates
Lambda expressions provide a concise way to define anonymous methods and are often used with delegates.
Example:
using System;
public delegate int Calculate(int x, int y);
public class Program
{
public static void Main(string[] args)
{
Calculate add = (x, y) => x + y;
Calculate multiply = (x, y) => x * y;
Console.WriteLine("Addition: " + add(3, 4));
Console.WriteLine("Multiplication: " + multiply(3, 4));
}
}
Explanation:
- Calculate Delegate: A delegate that takes two integers and returns an integer.
- Lambda Expressions: Define anonymous methods for addition and multiplication.
- Main Method: Executes the delegate methods.
Output:
Addition: 7 Multiplication: 12
Conclusion
Mastering delegates and events in C# allows you to create flexible and decoupled code. These features are powerful tools for implementing callbacks and building event-driven applications, enhancing the interactivity and responsiveness of your programs.
