C# Reference ยท Keywords and Statements
The delegate keyword
Reference for the C# delegate keyword: declare delegate types, use Func and Action, combine multicast delegates and understand invocation and null handling.
A delegate is a type that represents a reference to a method with a particular signature. You can store a method in a variable, pass it to another method, return it, and call it later. Delegates are the foundation for events, LINQ, callbacks and lambda expressions.
You declare a delegate type with the delegate keyword, but most of the time you use the built-in generic ones: Action (no return value), Func<..., TResult> (returns a value) and Predicate<T> (returns bool). A delegate instance can hold several methods at once (a multicast delegate), and calling it runs them in order.
delegate int Operation(int a, int b); Operation add = (a, b) => a + b; int sum = add(2, 3);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[modifiers] delegate ReturnType Name<T>(parameters) [where T : constraint]; Name d = Method; // method group conversion Name d = (x, y) => ...; // lambda d += Other; d -= Other; // combine / remove d(args); d.Invoke(args); d?.Invoke(args);
| Built-in type | Signature |
|---|---|
Action, Action<T1, ..., T16> |
Returns void. |
Func<T1, ..., T16, TResult> |
Returns TResult. |
Predicate<T> |
bool (T). |
Comparison<T> |
int (T, T), for sorting. |
EventHandler, EventHandler<TEventArgs> |
Convention for events. |
Errors: invoking a null delegate throws NullReferenceException. Delegate parameters and return types are checked at compile time; variance (in, out) allows assigning compatible delegate types.
Usage notes
- Multicast results: all handlers run in order, but only the last return value is returned. An exception in one handler stops the rest. Use
GetInvocationList()to call them yourself. - Delegates are immutable.
+=creates a new delegate that replaces the variable. This is not thread-safe on a shared field without care. - Closures: lambdas capture variables, and the compiler keeps them alive. In a loop,
foreachcreates a fresh variable per iteration but aforloop shares one. - Method groups:
list.ForEach(Console.WriteLine)converts a method group to a delegate. Since C# 10 a lambda or method group has a natural type when it can be inferred (var f = (int x) => x + 1;). - Static lambdas:
static x => x + 1forbids capturing and avoids allocations. - Performance: a delegate call is slightly slower than a direct call and allocates when a closure is created. Hot paths may prefer generics with interface constraints.
- Function pointers (
delegate*<int, int>) are unsafe and used for interop.