C# › Delegates, Events & Lambdas

What is a delegate in C#, and how does it enable passing methods as values?

Beginner

Answer

A delegate is a type-safe reference to one or more methods matching a specific signature, functioning like a type-checked function pointer -- assigning a method to a delegate lets you invoke that method indirectly through the delegate variable, and a single delegate can even hold multiple method references (a multicast delegate), invoking each in turn -- C# also provides three general-purpose built-in delegate types (Func<T> for methods returning a value, Action<T> for void methods, and Predicate<T> for boolean tests) that cover most everyday use cases without needing a custom delegate declaration.
delegate void MessageDelegate(string message);

void PrintMessage(string msg) => Console.WriteLine(msg);

MessageDelegate del = PrintMessage;
del("Hello, delegates!"); // invokes PrintMessage indirectly

// Multicast: both methods run in sequence
del += msg => Console.WriteLine("Also: " + msg);
del("Hi again");
Real-world example A logging framework accepts a Action<string> delegate parameter so callers can plug in their own custom log-writing behavior (console, file, remote endpoint) without the framework itself needing to know or care about the specific destination.

Common follow-ups: How do events build on top of delegates to add publish/subscribe semantics?;What's the difference between Func<T,R> and a custom delegate with the identical signature?