Лямбда-выражение с пустым входом

бросок exceptionhere;

Не так ли?

Пример, который я нашел, был

        if (args.Length == 0)
        {
            throw new ArgumentException("A start-up parameter is required.");
        }
30
задан Luk 2 October 2009 в 12:37
поделиться

4 ответа

Нулевым лямбда-эквивалентом будет () => 2 .

38
ответ дан 27 November 2019 в 23:29
поделиться

Это будет:

() => 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
19
ответ дан 27 November 2019 в 23:29
поделиться

Если у вас нет параметров, вы можете просто использовать ().

() => 2;
9
ответ дан 27 November 2019 в 23:29
поделиться

lmabda:

() => 2
4
ответ дан 27 November 2019 в 23:29
поделиться
Другие вопросы по тегам:

Похожие вопросы: