好吧,很愚蠢的问题。
x => x * 2
是代表同样的事情,作为一个代表拉姆达
int Foo(x) { return x * 2; }
但是,什么是拉姆达等价的
int Bar() { return 2; }
??
非常感谢!
好吧,很愚蠢的问题。
x => x * 2
是代表同样的事情,作为一个代表拉姆达
int Foo(x) { return x * 2; }
但是,什么是拉姆达等价的
int Bar() { return 2; }
??
非常感谢!
的无元lambda当量将是() => 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
你可以使用(),如果你有没有参数。
() => 2;
该lmabda是:
() => 2