-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
38 lines (35 loc) · 1.04 KB
/
Program.cs
File metadata and controls
38 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
namespace Iterators
{
class Program
{
//Ref: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/iterators
static void Main()
{
int[] numbers = new int[] { 3, 5, 8 };
foreach (int number in numbers)
{
Console.Write(number.ToString() + " ");
}
// Output: 3 5 8
Console.ReadKey();
foreach (int number in EvenSequence(5, 18))
{
Console.Write(number.ToString() + " ");
}
// Output: 6 8 10 12 14 16 18
Console.ReadKey();
}
public static System.Collections.Generic.IEnumerable<int> EvenSequence(int firstNumber, int lastNumber)
{
// Yield even numbers in the range.
for (int number = firstNumber; number <= lastNumber; number++)
{
if (number % 2 == 0)
{
yield return number;
}
}
}
}
}