Delegate: a type that represents a method (a type-safe function pointer).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // ====================================================================== // Q: Delegates, Func, Action, events and lambdas? // // - Delegate: a type that represents a method (a type-safe function pointer). // Func<T, TResult> returns a value, Action<T> does not, Predicate<T> returns // bool. // - Lambda: short syntax for a delegate or expression tree (x => x * 2). Lambdas // capture variables (closures), which costs an allocation. // - Event: a delegate that only the owning class can invoke. Others can only += // / -=. A forgotten -= on a long-lived publisher = memory leak. // ====================================================================== Func<decimal, decimal> withVat = net => net * 1.24m; Action<string> log = msg => Console.WriteLine(msg); log($"Total: {withVat(100)}"); |