/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/programming-guide/classes-and-structs/snippets/local-functions/IteratorWithoutLocal.cs
35 строк
1 KB
Tom Dykstra
Update snippets for NRT (#28622)
11 мар 2022, 19:03
Не верифицирован
11 мар 2022, 19:03
13fe3ed
Код
Авторство
О чём код?
public class IteratorWithoutLocalExample { public static void Main() { IEnumerable<int> xs = OddSequence(50, 110); Console.WriteLine("Retrieved enumerator..."); foreach (var x in xs) // line 11 { Console.Write($"{x} "); } } public static IEnumerable<int> OddSequence(int start, int end) { if (start < 0 || start > 99) throw new ArgumentOutOfRangeException(nameof(start), "start must be between 0 and 99."); if (end > 100) throw new ArgumentOutOfRangeException(nameof(end), "end must be less than or equal to 100."); if (start >= end) throw new ArgumentException("start must be less than end."); for (int i = start; i <= end; i++) { if (i % 2 == 1) yield return i; } } } // The example displays the output like this: // // Retrieved enumerator... // Unhandled exception. System.ArgumentOutOfRangeException: end must be less than or equal to 100. (Parameter 'end') // at IteratorWithoutLocalExample.OddSequence(Int32 start, Int32 end)+MoveNext() in IteratorWithoutLocal.cs:line 22 // at IteratorWithoutLocalExample.Main() in IteratorWithoutLocal.cs:line 11