Casting from IEnumerable to IEnumerator
我在玩
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 | class Animal { public string AnimalType { get; set; } public Animal(string animal_type) { AnimalType = animal_type; } } class FarmCollection { readonly Animal[] _farm = { new Animal("duck"), new Animal("cow"), new Animal("sheep") }; public IEnumerable<Animal> GetEnumerable() { foreach (Animal a in _farm) yield return a; } } class Test { public static void Main() { FarmCollection farm = new FarCollection(); IEnumerator<Animal> rator = (IEnumerator<Animal>)farm.GetEnumerable(); while (rator.MoveNext()) { Animal a = (Animal)rator.Current; Console.WriteLine(a.AnimalType); } } } |
第一个问题:为什么我没有输出,而main只是返回?第二个问题:为什么从
下面是您的
1 2 3 4 5 6 7 | public IEnumerable<Animal> GetEnumerable() { FarmCollection.<GetEnumerable>d__0 <GetEnumerable>d__ = new FarmCollection.<GetEnumerable>d__0(-2); <GetEnumerable>d__.<>4__this = this; return <GetEnumerable>d__; } |
类型
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | [CompilerGenerated] private sealed class <GetEnumerable>d__0 : IEnumerable<Animal>, IEnumerable, IEnumerator<Animal>, IEnumerator, IDisposable { private Animal <>2__current; private int <>1__state; private int <>l__initialThreadId; public FarmCollection <>4__this; public Animal 5__1; public Animal[] <>7__wrap3; public int <>7__wrap4; Animal IEnumerator<Animal>.Current { [DebuggerHidden] get { return this.<>2__current; } } object IEnumerator.Current { [DebuggerHidden] get { return this.<>2__current; } } [DebuggerHidden] IEnumerator<Animal> IEnumerable<Animal>.GetEnumerator() { FarmCollection.<GetEnumerable>d__0 <GetEnumerable>d__; if (Environment.CurrentManagedThreadId == this.<>l__initialThreadId && this.<>1__state == -2) { this.<>1__state = 0; <GetEnumerable>d__ = this; } else { <GetEnumerable>d__ = new FarmCollection.<GetEnumerable>d__0(0); <GetEnumerable>d__.<>4__this = this.<>4__this; } return <GetEnumerable>d__; } [DebuggerHidden] IEnumerator IEnumerable.GetEnumerator() { return this.System.Collections.Generic.IEnumerable<ConsoleApplication479.Animal>.GetEnumerator(); } bool IEnumerator.MoveNext() { bool result; try { switch (this.<>1__state) { case 0: this.<>1__state = -1; this.<>1__state = 1; this.<>7__wrap3 = this.<>4__this._farm; this.<>7__wrap4 = 0; goto IL_8D; case 2: this.<>1__state = 1; this.<>7__wrap4++; goto IL_8D; } goto IL_A9; IL_8D: if (this.<>7__wrap4 < this.<>7__wrap3.Length) { this.5__1 = this.<>7__wrap3[this.<>7__wrap4]; this.<>2__current = this.5__1; this.<>1__state = 2; result = true; return result; } this.<>m__Finally2(); IL_A9: result = false; } catch { this.System.IDisposable.Dispose(); throw; } return result; } [DebuggerHidden] void IEnumerator.Reset() { throw new NotSupportedException(); } void IDisposable.Dispose() { switch (this.<>1__state) { case 1: break; case 2: break; default: return; } this.<>m__Finally2(); } [DebuggerHidden] public <GetEnumerable>d__0(int <>1__state) { this.<>1__state = <>1__state; this.<>l__initialThreadId = Environment.CurrentManagedThreadId; } private void <>m__Finally2() { this.<>1__state = -1; } } |
所以在代码中,
现在,到第二个问题。注意,编译器生成的
注意这个类中的
因此,基本上,如果不执行
如果查看去糖代码,可以看到使用
重要的一行在getEnumerable()方法中:返回
所以,初始状态是-2,"还没有人请求枚举器状态"。如果调用