Introduction
In C#, Delegates and Events are core building blocks of event-driven programming — meaning actions happen in response to user interactions or specific triggers.
You use them daily when working with:
Button clicks (
button.Click += …)Background worker notifications
Real-time status updates
Logging or notification systems
Let’s understand how they work and then build a Windows Forms real-time application.
1. What is a Delegate?
A delegate is a type that holds a reference to a method.
You can think of it as a pointer to a function (type-safe).
Delegates allow:
Passing methods as parameters
Callback functionality
Decoupling components
Basic Example
public delegate void MyDelegate(string message);
class Program
{
static void ShowMessage(string msg)
{
Console.WriteLine(msg);
}
static void Main()
{
MyDelegate del = ShowMessage;
del("Hello from Delegate!");
}
}
Output
Hello from Delegate!
2. What is an Event?
An event is a wrapper around a delegate that enforces the publisher-subscriber pattern.
A publisher class defines an event.
A subscriber (listener) registers methods to respond when that event is triggered.
Example in daily life:
Think of a YouTube channel as a publisher, and its subscribers get notifications when a new video (event) is published.
3. Relationship Between Delegate and Event
| Concept | Description |
|---|---|
| Delegate | Defines the method signature that can be called. |
| Event | Restricts direct delegate access — allows only += (subscribe) and -= (unsubscribe). |
| Publisher | Class that raises (fires) the event. |
| Subscriber | Class or method that handles the event. |
4. Real-Time Example in Windows Forms
Let’s create a Windows Forms Application demonstrating how delegates and events can update the UI dynamically — simulating a file upload progress tracker.
Step 1: Create Delegate and Event
// Delegate declaration
public delegate void FileUploadHandler(int percentage);
// Publisher class
public class FileUploader
{
public event FileUploadHandler FileUploadProgress;
public void StartUpload()
{
for (int i = 0; i <= 100; i += 10)
{
System.Threading.Thread.Sleep(200); // Simulate time delay
OnFileUploadProgress(i);
}
}
protected virtual void OnFileUploadProgress(int percentage)
{
if (FileUploadProgress != null)
{
FileUploadProgress(percentage); // Raise event
}
}
}
Step 2: Design the Windows Form (Form1.cs)
Add Controls:
Button→btnStart→ Text = “Start Upload”ProgressBar→progressBar1Label→lblStatus

Join the conversation! Your thoughts help the community grow.