Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / tests / test-iter-05.cs
1 // Compiler options: -langversion:default
2
3 //
4 // Use
5
6 using System;
7 using System.Collections;
8
9 class X {
10         static IEnumerable GetIt ()
11         {
12                 List l = new List (3);
13                 l.Add (1);
14                 l.Add (2);
15                 l.Add (3);
16                 
17                 foreach (int i in l)
18                         yield return i;
19         }
20         
21         public static int Main ()
22         {
23                 int total = 0;
24                 foreach (int i in GetIt ()) {
25                         Console.WriteLine ("Got: " + i);
26                         total += i;
27                 }
28                 
29                 return total == 6 ? 0 : 1;
30         }
31 }
32
33 public class List : IEnumerable {
34
35         int pos = 0;
36         int [] items;
37         
38         public List (int i) 
39         {
40                 items = new int [i];
41         }
42         
43         public void Add (int value) 
44         {
45                 items [pos ++] = value;
46         }
47         
48         public MyEnumerator GetEnumerator ()
49         {
50                 return new MyEnumerator(this);
51         }
52         
53         IEnumerator IEnumerable.GetEnumerator ()
54         {
55                 return GetEnumerator ();
56         }
57         
58         public struct MyEnumerator : IEnumerator {
59                 
60                 List l;
61                 int p;
62                 
63                 public MyEnumerator (List l) 
64                 {
65                         this.l = l;
66                         p = -1;
67                 }
68                 
69                 public object Current {
70                         get {
71                                 return l.items [p];
72                         }
73                 }
74                 
75                 public bool MoveNext() 
76                 {
77                         return ++p < l.pos;
78                 }
79
80                 public void Reset() 
81                 {
82                         p = 0;
83                 }
84         }
85 }