skip to content
Khalid Abuhakmeh logo Khalid Abuhakmeh

Chain lambdas on one delegate

C# 3

Code card: Chain lambdas on one delegate. Program output: Hello World, .NET & Khalid

Delegates are multicast: += chains lambdas and one call runs them all, in order. It is available since C# 3. The key line is hello += () => Console.Write("World, ") in lambdas.cs. Run it and the output reads Hello World, .NET & Khalid.

Did this tip save you some time? Consider buying me a coffee to fuel the next one.

Buy me a coffee
lambdas.cs
Action hello = () => Console.Write("Hello ");
hello += () => Console.Write("World, ");
hello += () => Console.WriteLine(".NET & Khalid");
hello();
Output
$ dotnet run lambdas.cs
Hello World, .NET & Khalid