带有实例变量的 Lambda 如何工作?

时间:2021-06-13 19:44:44

标签: c# lambda

我通读了 this explanation of lambdas@mk.,它有以下示例:

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);

我多年来一直在 C# 中使用 lambda,但我不明白这里发生了什么。显然这添加了一个实例变量,但是声明中的什么定义了所有这些? x 或 y 是实例变量吗?底部的 Closure 示例是什么?

0 个答案:

没有答案
相关问题