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