Delegates, Action, Func, Predicate & Events Masterclass

โšก C# 12 & .NET 8 ๐ŸŸข Chapter 21 of 35 ๐Ÿ“‚ Phase 7: Advanced C# Language ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter: Delegates ยท Func ยท Action ยท Predicate ยท Anonymous Methods ยท Events ยท Publisher-Subscriber Pattern ยท EventArgs ยท Unsubscribing

Welcome to Phase 7 (Chapter 21): C# Delegates, Built-in Delegates (Func, Action, Predicate) & Events Masterclass! A delegate is a type-safe function pointer reference to a method with a specific signature. In this chapter, we master custom delegates, built-in delegates (Action, Func, Predicate), anonymous methods, event publisher-subscriber design patterns, custom event arguments, and event unsubscribing.

1Delegate Ante Enti? Built-in Delegates (Func, Action, Predicate)

A delegate allows passing methods as parameters to other methods. .NET provides three built-in generic delegates to avoid declaring custom delegate types:

DelegateReturn TypeUsage DescriptionExample Signature
Action<T1, T2>void (No return value)Encapsulates a method that returns no result.Action<string> log = msg => Console.WriteLine(msg);
Func<T1, T2, TResult>TResult (Last parameter)Encapsulates a method that returns a result value.Func<int, int, int> add = (a, b) => a + b;
Predicate<T>boolEncapsulates a method that tests a condition.Predicate<int> isEven = n => n % 2 == 0;
C# โ€” Func, Action & Predicate Code โ–ถ Run in Compiler
// 1. Func โ€” Takes two ints, returns int
Func<int, int, int> add = (first, second) => first + second;
Console.WriteLine($"Func Add: {add(10, 20)}"); // 30

// 2. Action โ€” Takes string, returns void
Action<string> printMessage = msg => Console.WriteLine($"LOG: {msg}");
printMessage("Delegates in C# are powerful!");

// 3. Predicate โ€” Takes int, returns bool
Predicate<int> isPositive = num => num > 0;
Console.WriteLine($"Is 15 positive: {isPositive(15)}");
2Events & Publisher-Subscriber Pattern

Events provide a notification system where a Publisher class triggers an event, and one or more Subscriber classes receive and handle that event asynchronously.

C# โ€” Event Publisher & Subscriber โ–ถ Run in Compiler
public class ProcessPublisher
{
    // Declare Event using EventHandler delegate
    public event EventHandler? ProcessCompleted;

    public void StartProcess()
    {
        Console.WriteLine("Process started...");
        System.Threading.Thread.Sleep(500); // Simulate work
        OnProcessCompleted();
    }

    protected virtual void OnProcessCompleted()
    {
        ProcessCompleted?.Invoke(this, EventArgs.Empty); // Safe event invocation
    }
}

// Subscriber Code
ProcessPublisher publisher = new();
publisher.ProcessCompleted += (sender, e) => Console.WriteLine("Subscriber received: Process Finished!");
publisher.StartProcess();
3Technical FAQs

Q1: Why should I unsubscribe from events (-=)?

If a subscriber does not unsubscribe from a long-lived publisher event, the publisher holds a reference pointer to the subscriber, preventing the Garbage Collector from freeing subscriber memory (causing memory leaks).