C# Tutorial: Delegates and Events
Delegates as type-safe function pointers, Func and Action, events, and lambdas.
Functions as first-class citizens
Delegates are type-safe function pointers. They allow methods to be passed as parameters and stored in variables.
`Func` and `Action` are built-in delegates you use in practice. You rarely define custom delegates.
What you'll learn in this C# delegates and events tutorial
This interactive C# tutorial has 8 hands-on exercises. Estimated time: 20 minutes.
- Func<T, TResult> — `Func<TIn, TOut>` is a built-in delegate for a function that takes input and returns output.
- Action<T> — `Action<T>` is a delegate for a function that returns nothing (void).
- Passing functions to methods — Accept delegates as parameters for flexible, reusable code.
- Multicast delegates — A delegate can hold multiple methods and invoke them all.
- Events — Events use delegates but restrict callers to += and -= only.
- EventHandler pattern — The standard .NET event pattern uses EventHandler<TEventArgs>.
- Closures in lambdas — Lambdas capture variables from their enclosing scope.
- Predicate<T> — `Predicate<T>` is a built-in delegate for bool-returning functions.
C# Delegates and Events concepts covered
- Functions as first-class citizens