Created
September 8, 2012 20:15
-
-
Save rednaxelafx/3679356 to your computer and use it in GitHub Desktop.
Simple example of C# iterator (generator) decompiled. Decompiled with ILSpy; turned off decompilation for iterators to see details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Runtime.CompilerServices; | |
using System.Threading; | |
internal class Test | |
{ | |
[CompilerGenerated] | |
private sealed class <GetNaturals>d__0 : IEnumerable<int>, IEnumerable, IEnumerator<int>, IEnumerator, IDisposable | |
{ | |
private int <>2__current; | |
private int <>1__state; | |
public int <i>5__1; | |
int IEnumerator<int>.Current | |
{ | |
[DebuggerHidden] | |
get | |
{ | |
return this.<>2__current; | |
} | |
} | |
object IEnumerator.Current | |
{ | |
[DebuggerHidden] | |
get | |
{ | |
return this.<>2__current; | |
} | |
} | |
[DebuggerHidden] | |
IEnumerator<int> IEnumerable<int>.GetEnumerator() | |
{ | |
Test.<GetNaturals>d__0 result; | |
if (Interlocked.CompareExchange(ref this.<>1__state, 0, -2) == -2) | |
{ | |
result = this; | |
} | |
else | |
{ | |
result = new Test.<GetNaturals>d__0(0); | |
} | |
return result; | |
} | |
[DebuggerHidden] | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return this.System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator(); | |
} | |
bool IEnumerator.MoveNext() | |
{ | |
bool result; | |
switch (this.<>1__state) | |
{ | |
case 0: | |
this.<>1__state = -1; | |
this.<i>5__1 = 1; | |
break; | |
case 1: | |
this.<>1__state = -1; | |
break; | |
default: | |
result = false; | |
return result; | |
} | |
this.<>2__current = this.<i>5__1++; | |
this.<>1__state = 1; | |
result = true; | |
return result; | |
} | |
[DebuggerHidden] | |
void IEnumerator.Reset() | |
{ | |
throw new NotSupportedException(); | |
} | |
void IDisposable.Dispose() | |
{ | |
} | |
[DebuggerHidden] | |
public <GetNaturals>d__0(int <>1__state) | |
{ | |
this.<>1__state = <>1__state; | |
} | |
} | |
private static void Main(string[] args) | |
{ | |
IEnumerable<int> naturals = Test.GetNaturals(); | |
foreach (int current in naturals) | |
{ | |
Console.WriteLine(current); | |
if (current > 3) | |
{ | |
break; | |
} | |
} | |
} | |
private static IEnumerable<int> GetNaturals() | |
{ | |
return new Test.<GetNaturals>d__0(-2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class NaturalsIterator : | |
IEnumerable<int>, IEnumerable, | |
IEnumerator<int>, IEnumerator, IDisposable { | |
int _current, _state, _i; | |
public NaturalsIterator(int state) { | |
_state = state; | |
} | |
int IEnumerator<int>.Current { | |
get { return _current; } | |
} | |
bool IEnumerator.MoveNext() { | |
switch (_state) { | |
case 0: | |
_i = 1; | |
break; | |
case 1: | |
break; | |
default: | |
return false; | |
} | |
_current = _i++; | |
_state = 1; | |
return true; | |
} | |
} | |
private static IEnumerable<int> GetNaturals() { | |
return new NaturalsIterator(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
class Test { | |
static void Main(string[] args) { | |
IEnumerable<int> naturals = GetNaturals(); | |
foreach (int i in naturals) { | |
Console.WriteLine(i); | |
if (i > 3) break; | |
} | |
} | |
static IEnumerable<int> GetNaturals() { | |
int i = 1; | |
while (true) { | |
yield return i++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment