- это лямбда, представляющая то же самое, что и делегат для
int Foo(x) { return x * 2; }
Но что такое лямбда-эквивалент
int Bar() { return 2; }
??
Спасибо большое!
Ответ 1
Нулевой эквивалент лямбда будет () => 2.
Ответ 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
Ответ 3
Вы можете просто использовать(), если у вас нет параметров.