C# Delegates & Events Cheat Sheet
Covers delegates, multicast delegates, the built-in Func, Action, and Predicate types, and the C# event pattern for publish-subscribe notifications.
2 PagesIntermediateMar 30, 2026
Delegates
Type-safe references to methods, including multicast.
csharp
public delegate int MathOperation(int a, int b); // Declare a delegate typeint Add(int a, int b) => a + b;int Multiply(int a, int b) => a * b;MathOperation op = Add;Console.WriteLine(op(3, 4)); // 7op = Multiply;Console.WriteLine(op(3, 4)); // 12// Multicast delegateAction<string> logger = Console.WriteLine;logger += msg => File.AppendAllText("log.txt", msg);logger("Hello"); // Invokes both handlers in order
Func, Action, Predicate
Built-in generic delegate types for common shapes.
csharp
Func<int, int, int> add = (a, b) => a + b; // Takes params, returns a valueAction<string> print = msg => Console.WriteLine(msg); // Takes params, returns voidPredicate<int> isEven = n => n % 2 == 0; // Takes a param, returns boolint sum = add(2, 3);print("done");bool result = isEven(4);
Events
The standard publish-subscribe pattern.
csharp
public class Button{ public event EventHandler<EventArgs>? Clicked; // Standard event pattern public void SimulateClick() { Clicked?.Invoke(this, EventArgs.Empty); // Null-safe raise }}var button = new Button();button.Clicked += (sender, e) => Console.WriteLine("Button clicked!");button.SimulateClick();
Key Concepts
Terminology for delegates and events.
- delegate- A type-safe reference to one or more methods with a matching signature
- Multicast delegate- Combining delegates with += invokes each one in sequence when called
- event keyword- Restricts external code to += / -= only, preventing direct invocation or overwrite
- EventHandler / EventHandler<T>- Standard delegate signature: (object? sender, EventArgs e)
- Func<T,...,TResult>- Built-in generic delegate for methods that return a value
- Action<T,...>- Built-in generic delegate for methods that return void
- Null-conditional invoke- handler?.Invoke(...) avoids a NullReferenceException when there are no subscribers
Pro Tip
Always raise events with the null-conditional operator (Clicked?.Invoke(...)) instead of a separate null check — it's safe against a subscriber unsubscribing between the check and the call.
Was this cheat sheet helpful?
Explore Topics
#CDelegatesEvents#CDelegatesEventsCheatSheet#Programming#Intermediate#Delegates#FuncActionPredicate#Events#KeyConcepts#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance