Delegates and Events
Learn how delegates and events enable communication between objects in C#.
Delegates and Events is a free C# Academy lesson on CoddyKit — lesson 5 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Delegates and Events
Welcome to the next lesson! In this lesson, you’ll learn about delegates and events in C#, their purpose, and how they enable event-driven programming. Let’s get started!

2
What is a Delegate?
A delegate is a type that represents a reference to a method. It allows methods to be passed as parameters and invoked dynamically.
Key Point: Delegates are essential for implementing events and callback functions in C#.
3
Defining and Using Delegates
To define a delegate, use the delegate keyword. Example:
using System;
class Program {
// Define a delegate
public delegate void MessageDelegate(string message);
static void DisplayMessage(string message) {
Console.WriteLine(message);
}
static void Main() {
// Create a delegate instance
MessageDelegate msg = DisplayMessage;
// Invoke the delegate
msg("Hello, Delegates!");
}
}
4
Multicast Delegates
A single delegate can point to multiple methods, and all methods will be invoked in order. Example:
Tip: Use multicast delegates for scenarios where multiple actions need to be performed.
using System;
class Program {
public delegate void MessageDelegate(string message);
static void ShowMessage(string message) {
Console.WriteLine("Message: " + message);
}
static void LogMessage(string message) {
Console.WriteLine("Log: " + message);
}
static void Main() {
MessageDelegate msg = ShowMessage;
msg += LogMessage; // Add another method
msg("Hello, Multicast Delegates!"); // Both methods are invoked
}
}
5
What is an Event?
An event is a mechanism that allows a class to notify other classes or objects when something of interest occurs. Events are built on top of delegates.
Key Point: Events enable loose coupling between publishers and subscribers in event-driven programming.
6
Defining and Using Events
To define an event, use the event keyword with a delegate type. Example:
Key Point: Events allow you to build reactive and event-driven systems.
using System;
class Publisher {
public delegate void NotifyDelegate(string message);
public event NotifyDelegate Notify; // Define an event
public void RaiseEvent(string message) {
Notify?.Invoke(message); // Invoke the event
}
}
class Subscriber {
public void OnNotify(string message) {
Console.WriteLine("Subscriber received: " + message);
}
}
class Program {
static void Main() {
Publisher pub = new Publisher();
Subscriber sub = new Subscriber();
pub.Notify += sub.OnNotify; // Subscribe to the event
pub.RaiseEvent("Hello, Events!"); // Raise the event
}
}
7
Unsubscribing from Events
Subscribers can unsubscribe from events using the -=` operator. Example:
Tip: Always unsubscribe from events to avoid memory leaks.
using System;
class Publisher {
public delegate void NotifyDelegate(string message);
public event NotifyDelegate Notify;
public void RaiseEvent(string message) {
Notify?.Invoke(message);
}
}
class Subscriber {
public void OnNotify(string message) {
Console.WriteLine("Subscriber received: " + message);
}
}
class Program {
static void Main() {
Publisher pub = new Publisher();
Subscriber sub = new Subscriber();
pub.Notify += sub.OnNotify; // Subscribe
pub.RaiseEvent("First Message");
pub.Notify -= sub.OnNotify; // Unsubscribe
pub.RaiseEvent("Second Message"); // No output, as subscriber is removed
}
}
8
Practice Challenge
Create a delegate CalculationDelegate for arithmetic operations, and an event OnCalculate. Write a program that raises the event and performs calculations. Example:
using System;
class Calculator {
public delegate void CalculationDelegate(double result);
public event CalculationDelegate OnCalculate;
public void Add(double a, double b) {
double result = a + b;
OnCalculate?.Invoke(result);
}
}
class Program {
static void Main() {
Calculator calc = new Calculator();
// Subscribe to the event
calc.OnCalculate += result => Console.WriteLine("Result: " + result);
calc.Add(5, 3); // Outputs: Result: 8
}
}
9
10
Great Job!
Congratulations! You’ve learned how to use delegates and events to build dynamic and event-driven programs. In the next lesson, we’ll explore working with collections like lists, dictionaries, and queues. Let’s keep coding!

Frequently asked questions
Is the “Delegates and Events” lesson free?
Yes — the full text of “Delegates and Events” is free to read here on the web, and the C# Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Delegates and Events”?
Learn how delegates and events enable communication between objects in C#. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 5 of 6, so you can start here or from the beginning and move at your own pace.
How long does the “Delegates and Events” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.