0

I read through this explanation of lambdas by @mk. and it has the following example:

Func<int, Func<int, int>> adder = (int x) => (int y) => x + y; // `int` declarations optional Func<int, int> add5 = adder(5); var add6 = adder(6); // Using implicit typing Debug.Assert(add5(1) == 6); Debug.Assert(add6(-1) == 5); // Closure example int yEnclosed = 1; Func<int, int> addWithClosure = (x) => x + yEnclosed; Debug.Assert(addWithClosure(2) == 3); 

I've used lambdas in C# for years and yet I do not understand what is going on here. Obviously this adds an instance variable, but what in the declaration is defining all this? Is x or y the instance variable? And what's with the Closure example at the bottom?

2
  • 1
    Its a delegate that returns a delegate, right? Commented Jun 13, 2021 at 19:48
  • How to use closures in C# Commented Jun 13, 2021 at 20:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.