C# Mastery

Delegates, Func, Action, and Predicates

1 Views Updated 5/4/2026

Functional C#: Delegates & Actions

A Delegate is a type-safe function pointer. It allows you to pass a "Method" as a parameter to another method. This is the foundation of LINQ, Events, and modern asynchronous callbacks.

1. The Trio: Func, Action, and Predicate

You rarely need to define custom delegates anymore. Microsoft provided three generic wrappers that cover 99% of use cases.

  • Action: For methods that return void.
  • Func<T, TResult>: For methods that return a value.
  • Predicate<T>: A Func that specifically returns a bool.
// 1. An action that takes a string
Action<string> log = msg => Console.WriteLine(msg);

// 2. A func that takes two ints and returns an int
Func<int, int, int> add = (a, b) => a + b;

// 3. Using them as parameters
public void ProcessData(int val, Action<int> callback) 
{
    callback(val * 2);
}

2. Multicast Delegates

A single delegate can hold references to multiple methods. When you call the delegate, all methods fire in sequence. This is how the Observer Pattern is implemented in C# via Events.

4. Interview Mastery

Q: "What is a Closure in C# Lambda expressions?"

Architect Answer: "A Closure occurs when a lambda expression 'captures' a variable from its outer scope. Even if that outer variable is a local variable on the stack that should have been destroyed, the C# compiler cleverly generates a hidden class to store that variable on the heap, ensuring it stays alive as long as the lambda exists. Be careful: capturing large objects in closures can prevent the Garbage Collector from cleaning them up, leading to memory leaks."

C# Mastery
1. Modern C# & Framework Fundamentals
Introduction to the .NET Ecosystem & Runtime (JIT, CLR) C# 12/13 Top-Level Statements & Global Usings Variables, Data Types, and Value vs Reference Deep Dive Mastering Nullable Reference Types & Null Safety The Magic of Strings (Interpolation, Verbatim, and Immutability)
2. Control Flow & Logical Structures
Advanced Pattern Matching (Switch Expressions) The Precision of Numbers: Checked vs Unchecked Math Iterators: Foreach, Yield Return, and Deferred Execution Defensive Programming: Guard Clauses and Exception Mastery
3. Object-Oriented Mastery
Classes vs Structs vs Records (Which to use when?) Mastering Primary Constructors & Object Initializers Interface Architectures: Default Implementations & Segregation Polymorphism vs Composition (The Architect's Dilemma) Partial Classes, Extension Methods, and Static Classes
4. Functional C# & Collections
Delegates, Func, Action, and Predicates Lambda Expressions & The Evolution of Linq Dynamic Array Management: List<T>, Dictionary, and HashSet Custom Collections & Yielding Data streams
5. Asynchronous & Parallel Programming
Async/Await Deep Dive: Task Lifecycle & Thread Safety Synchronization Context & Avoiding Deadlocks Parallel.ForEach vs PLINQ vs Tasks CancellationToken Mastery: Surgically Aborting Operations
6. Advanced Engineering & High Performance
Generics & Constraints: Building Type-Safe Libraries Reflection and Attributes: Reading Metadata at Runtime Dependency Injection Internals (ServiceCollection from scratch) High-Performance Memory: Span<T> and ReadOnlySpan<T> Garbage Collection Segments & LOH Internals Managing Unmanaged Resources: IDisposable & Finalizers Introduction to Source Generators & Interceptors C# Interview Masterclass: Architect-Level Explanations