From the course: C#: Delegates, Events, and Lambdas

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Using composable delegates

Using composable delegates - C# Tutorial

From the course: C#: Delegates, Events, and Lambdas

Using composable delegates

- [Teacher] One interesting feature of delegates that we will explore in this lesson is that they are composable. In other words, you can chain delegates together and each one will be called in the order that it was added to the chain. So let's look at a conceptual example. Suppose I declare a delegate type that takes two arguments and returns a result, and then I write three delegate implementations, D1, D2, and D3, that all have the same function signature. Now in my code, I assign each of these functions, two delegate variables, okay, F1, F2, and F3. Then I can chain them together using the plus operator or the plus equals operator. And in this case, I've assigned this chain to the delegate named All. So now the All delegate combines the F1, F2, and F3 delegate functions. So once I've done this and I invoke the all delegate function, then each of these delegates will be called in order. And similarly I can use the…

Contents