lambda表达式与空隙输入(Lambda expression with a void input

2019-08-17 04:43发布

好吧,很愚蠢的问题。

x => x * 2

是代表同样的事情,作为一个代表拉姆达

int Foo(x) { return x * 2; }

但是,什么是拉姆达等价的

int Bar() { return 2; }

??

非常感谢!

Answer 1:

的无元lambda当量将是() => 2



Answer 2:

这将是:

() => 2

实例:

var list = new List<int>(Enumerable.Range(0, 10));
Func<int> x = () => 2;
list.ForEach(i => Console.WriteLine(x() * i));

正如意见中的要求,这里的上述样品的细分...

// initialize a list of integers. Enumerable.Range returns 0-9,
// which is passed to the overloaded List constructor that accepts
// an IEnumerable<T>
var list = new List<int>(Enumerable.Range(0, 10));

// initialize an expression lambda that returns 2
Func<int> x = () => 2;

// using the List.ForEach method, iterate over the integers to write something
// to the console.
// Execute the expression lambda by calling x() (which returns 2)
// and multiply the result by the current integer
list.ForEach(i => Console.WriteLine(x() * i));

// Result: 0,2,4,6,8,10,12,14,16,18


Answer 3:

你可以使用(),如果你有没有参数。

() => 2;


Answer 4:

该lmabda是:

() => 2


文章来源: Lambda expression with a void input
标签: c# c#-3.0 lambda