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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
//
// Tests the foreach on strings, and tests the implicit use of foreach
// to pull the enumerator from the class and identify the pattern to be called
//
using System;
using System.Collections;
class Y {
int count = 0;
public bool MoveNext ()
{
count++;
return count != 10;
}
public object Current {
get {
return count;
}
}
}
class X {
static string [] a = {
"one", "two", "three"
};
public Y GetEnumerator ()
{
return new Y ();
}
static int Main ()
{
//
// String test
//
string total = "";
foreach (string s in a){
total = total + s;
}
if (total != "onetwothree")
return 1;
//
// Pattern test
//
X x = new X ();
int t = 0;
foreach (object o in x){
t += (int) o;
}
if (t != 45)
return 2;
//
// Looking for GetEnumerator on interfaces test
//
Hashtable xx = new Hashtable ();
xx.Add ("A", 10);
xx.Add ("B", 20);
IDictionary vars = xx;
string total2 = "";
foreach (string name in vars.Keys){
total2 = total2 + name;
}
if ((total2 != "AB") && (total2 != "BA"))
return 3;
ArrayList list = new ArrayList ();
list.Add ("one");
list.Add ("two");
list.Add ("three");
int count = 0;
//
// This test will make sure that `break' inside foreach will
// actually use a `leave' opcode instead of a `br' opcode
//
foreach (string s in list){
if (s == "two"){
break;
}
count++;
}
if (count != 1)
return 4;
Console.WriteLine ("test passes");
return 0;
}
}
|