Delegates and Events in C#: A Comprehensive Guide with Examples
Delegates and events are essential components of C# programming, enabling flexible and powerful event-driven application design. This guide will explain delegates and events with clear examples and use cases.
What is a Delegate?
A delegate is a type that represents references to methods with a specific signature. Delegates are used to pass methods as arguments, making them useful for callback methods and designing extensible and flexible applications.
Basic Delegate Example
using System;
// Define a delegate
public delegate void DisplayMessage(string message);
class Program {
// Method matching the delegate signature
public static void ShowMessage(string message) {
Console.WriteLine(message);
}
static void Main() {
// Instantiate the delegate
DisplayMessage del = ShowMessage;
// Call the delegate
del("Hello, Delegates!");
}
}
What is an Event?
An event in C# is a mechanism that allows objects to communicate with each other. Events rely on delegates to bind methods, making them integral to event-driven programming.
Basic Event Example
using System;
// Define a delegate
public delegate void Notify();
class Process {
// Define an event using the delegate
public event Notify ProcessCompleted;
public void StartProcess() {
Console.WriteLine("Process Started...");
// Simulate process completion
System.Threading.Thread.Sleep(2000);
OnProcessCompleted();
}
protected virtual void OnProcessCompleted() {
// Invoke the event
ProcessCompleted?.Invoke();
}
}
class Program {
static void Main() {
Process process = new Process();
// Subscribe to the event
process.ProcessCompleted += () => Console.WriteLine("Process Completed!");
process.StartProcess();
}
}
Multicast Delegates
A multicast delegate can point to multiple methods. When invoked, all methods referenced by the delegate are called sequentially.
Example of Multicast Delegate
using System;
public delegate void Operations(int x, int y);
class Calculator {
public static void Add(int x, int y) {
Console.WriteLine($"Addition: {x + y}");
}
public static void Multiply(int x, int y) {
Console.WriteLine($"Multiplication: {x * y}");
}
}
class Program {
static void Main() {
Operations ops = Calculator.Add;
ops += Calculator.Multiply;
ops(5, 10);
// Output:
// Addition: 15
// Multiplication: 50
}
}
Real-World Example
Event for Button Click Simulation
using System;
public delegate void ButtonClickHandler();
class Button {
public event ButtonClickHandler Click;
public void OnClick() {
Click?.Invoke();
}
}
class Program {
static void Main() {
Button button = new Button();
// Subscribe to the Click event
button.Click += () => Console.WriteLine("Button clicked!");
button.OnClick(); // Output: Button clicked!
}
}
This Content Sponsored by Buymote Shopping app BuyMote E-Shopping Application is One of the Online Shopping App Now Available on Play Store & App Store (Buymote E-Shopping) Click Below Link and Install Application: https://buymote.shop/links/0f5993744a9213079a6b53e8 Sponsor Content: #buymote #buymoteeshopping #buymoteonline #buymoteshopping #buymoteapplication
0 Comments