
Mastering Delegates, Events, and Lambda Expressions in C#: A Comprehensive Guide
Feb 6
3 min read
0
0
0
Introduction
C# offers powerful tools for handling function references and event-driven programming. Three essential concepts—Delegates, Events, and Lambda Expressions—work together to enable flexible, reusable, and efficient code. These concepts are frequently tested in C# technical interviews, making them crucial for every developer to understand. This blog covers these key C# interview topics in depth.
In this blog, we’ll explore Delegates, Events, and Lambda Expressions, explain how they function individually, and demonstrate how they work together in real-world applications.
1. Understanding Delegates in C#
A delegate is a type that holds references to methods with a matching signature, allowing function pointers, callbacks, and event handling in C#.
Declaring a Delegate
A delegate is defined using the delegate keyword.
public delegate void MyDelegate(string message);
This defines a delegate type that refers to any method with a void return type and a string parameter.
Using a Delegate
class Program
{
static void ShowMessage(string message)
{
Console.WriteLine("Message: " + message);
}
static void Main()
{
MyDelegate del = new MyDelegate(ShowMessage);
del("Hello, Delegates!"); // Output: Message: Hello, Delegates!
}
}
Multicast Delegates
Delegates can reference multiple methods. When invoked, all methods in the invocation list execute.
void Method1(string msg) => Console.WriteLine("Method1: " + msg);
void Method2(string msg) => Console.WriteLine("Method2: " + msg);
MyDelegate del = Method1;
del += Method2;
del("Test");
// Output:
// Method1: Test
// Method2: Test
2. Events in C#
Events build upon delegates, providing a structured way to implement the observer pattern. Events ensure that subscribers can add or remove handlers without directly invoking them.
Declaring an Event
An event is declared using the event keyword with a delegate type.
public delegate void Notify();
class Process
{
public event Notify OnProcessCompleted; // Declare event
public void Start()
{
Console.WriteLine("Process Started...");
OnProcessCompleted?.Invoke(); // Raise event if subscribers exist
}
}
Subscribing to an Event
class Program
{
static void ShowMessage()
{
Console.WriteLine("Process Completed Successfully.");
}
static void Main()
{
Process process = new Process();
process.OnProcessCompleted += ShowMessage; // Subscribe
process.Start();
}
}
Why Use Events Instead of Delegates?
Events prevent external invocation.
They enforce one-way communication (only publishers can raise them).
They provide better encapsulation and maintainability.
3. Lambda Expressions in C#
Lambda expressions provide a concise way to define inline functions, often used in LINQ, event handling, and functional programming.
Basic Syntax
A lambda expression follows this syntax:
(parameter) => expression
Example:
Func<int, int> square = x => x * x;
Console.WriteLine(square(5)); // Output: 25
Using Lambdas with Delegates
Instead of defining a separate method, we can use a lambda expression:
MyDelegate del = msg => Console.WriteLine("Lambda: " + msg);
del("Hello");
Using Lambdas with Events
Lambdas simplify event subscriptions:
process.OnProcessCompleted += () => Console.WriteLine("Lambda: Process Finished!");
4. How They Work Together
1. Delegates Enable Event Handling
Events depend on delegates to notify multiple subscribers.
public delegate void ProcessEventHandler(string status);
class Process
{
public event ProcessEventHandler ProcessCompleted;
public void Start()
{
Console.WriteLine("Process Started...");
ProcessCompleted?.Invoke("Success");
}
}
class Program
{
static void Main()
{
Process process = new Process();
process.ProcessCompleted += status => Console.WriteLine("Process ended with status: " + status);
process.Start();
}
}
2. Lambdas Provide Cleaner Code
Lambdas reduce unnecessary method declarations.
Without lambda:
void ShowStatus(string status)
{
Console.WriteLine("Process ended with: " + status);
}
process.ProcessCompleted += ShowStatus;
With lambda:
process.ProcessCompleted += status => Console.WriteLine("Process ended with: " + status);
3. Real-World Use Case: Event-Driven UI
In Windows Forms and WPF, events are essential for UI interactions.
Button btn = new Button();
btn.Click += (s, e) => Console.WriteLine("Button Clicked!");
Here:
The Click event uses a delegate.
A lambda expression simplifies the subscription.
5. Key Takeaways for C# Interviews
Delegates allow method references and enable callbacks.
Events provide a structured way to notify multiple subscribers.
Lambda Expressions simplify delegate and event usage.
They work together to enable event-driven programming and clean, maintainable code.
Conclusion
Mastering Delegates, Events, and Lambda Expressions is essential for C# interviews and real-world applications. These concepts promote flexibility, modularity, and responsiveness in software design. Make sure to practice coding examples, as these topics are frequently covered in C# interview topics