forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforeach-in_1.cs
More file actions
73 lines (68 loc) · 1.69 KB
/
Copy pathforeach-in_1.cs
File metadata and controls
73 lines (68 loc) · 1.69 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class ForEachTest
{
static void Main(string[] args)
{
ExampleOne();
ExampleTwo();
ExampleThree();
}
private static void ExampleOne() // 12 - 26
{
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
foreach (int element in fibarray)
{
System.Console.WriteLine(element);
}
System.Console.WriteLine();
// Output:
// 0
// 1
// 1
// 2
// 3
// 5
// 8
// 13
}
private static void ExampleTwo() // 31-46
{
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
// Compare the previous loop to a similar for loop.
for (int i = 0; i < fibarray.Length; i++)
{
System.Console.WriteLine(fibarray[i]);
}
System.Console.WriteLine();
// Output:
// 0
// 1
// 1
// 2
// 3
// 5
// 8
// 13
}
private static void ExampleThree() // 51 - 69
{
int[] fibarray = new int[] { 0, 1, 1, 2, 3, 5, 8, 13 };
// You can maintain a count of the elements in the collection.
int count = 0;
foreach (int element in fibarray)
{
count += 1;
System.Console.WriteLine("Element #{0}: {1}", count, element);
}
System.Console.WriteLine("Number of elements in the array: {0}", count);
// Output:
// Element #1: 0
// Element #2: 1
// Element #3: 1
// Element #4: 2
// Element #5: 3
// Element #6: 5
// Element #7: 8
// Element #8: 13
// Number of elements in the array: 8
}
}