C# Events and Delegates

Introduction to Events and Delegates

C# events and delegates are powerful tools that enable developers to create dynamic and responsive applications. Events facilitate communication between different components, while delegates act as function pointers, allowing for indirect method invocation. Together, these features enhance code modularity, flexibility, and maintainability.

Event Handling in C#

Event handling is a fundamental concept in C#, allowing classes to respond to events raised by other classes. The process involves three key components: the event sender, the event handler, and the event itself. When an event occurs, the sender triggers it, and any registered event handlers respond accordingly.

Example:

public class VideoPlayer
{
// Define a delegate for the event
public delegate void VideoEventHandler(object sender, EventArgs e);

// Declare the event based on the delegate
public event VideoEventHandler VideoEnded;

public void Play()
{
// Simulate video playback
Console.WriteLine(“Playing video…”);

// When the video ends, raise the event
OnVideoEnded();
}

protected virtual void OnVideoEnded()
{
// Raise the event
VideoEnded?.Invoke(this, EventArgs.Empty);
}
}

public class NotificationService
{
public void SendNotification(object sender, EventArgs e)
{
Console.WriteLine(“Sending notification: Video has ended.”);
}
}

In this example, the VideoPlayer class raises the VideoEnded event when the video playback ends. The NotificationService class registers an event handler to respond to this event.

Creating Custom Events and Delegates

C# empowers developers to create custom events and delegates tailored to specific needs. This customization allows for the design of event-driven systems that precisely fit the application requirements.

Example:

public class StockMarket
{
public delegate void StockPriceChangedEventHandler(object sender, StockPriceChangedEventArgs e);

public event StockPriceChangedEventHandler StockPriceChanged;

public void UpdateStockPrice(string stockSymbol, decimal newPrice)
{
// Update the stock price and raise the event
OnStockPriceChanged(new StockPriceChangedEventArgs(stockSymbol, newPrice));
}

protected virtual void OnStockPriceChanged(StockPriceChangedEventArgs e)
{
StockPriceChanged?.Invoke(this, e);
}
}

public class StockPriceChangedEventArgs : EventArgs
{
public string StockSymbol { get; }
public decimal NewPrice { get; }

public StockPriceChangedEventArgs(string stockSymbol, decimal newPrice)
{
StockSymbol = stockSymbol;
NewPrice = newPrice;
}
}

In this example, the StockMarket class raises a custom StockPriceChanged event whenever the stock price is updated. The StockPriceChangedEventArgs class holds relevant data about the stock symbol and the new price.

Conclusion:

C# events and delegates are powerful concepts that enable event-driven programming and seamless communication between different components in an application. By understanding event handling and utilizing custom events and delegates, developers can build applications that respond dynamically to specific occurrences, adapt to changing scenarios, and provide a smooth user experience.

Embracing the power of C# events and delegates empowers programmers to create responsive, scalable, and efficient applications. So, dive into the world of event-driven programming with C#, and unlock the full potential of your applications!

Leave a Comment