Last active
January 12, 2019 19:37
-
-
Save natalie-o-perret/041df0e8b6e618ae9ac80b5170a31198 to your computer and use it in GitHub Desktop.
C# yield keyword compilation rewriting example
This file contains hidden or 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.Linq; | |
using System.Collections.Generic; | |
public class Example | |
{ | |
private int _iAmAHere = 0; | |
public IEnumerable<int> DoSomething() | |
{ | |
var start = 1; | |
var stop = 42; | |
var breakCondition = 34; | |
var exceptionCondition = 41; | |
var multiplier = 2; | |
if (multiplier == 2) | |
{ | |
Console.WriteLine("multiplier has been checked"); | |
} | |
if (_iAmAHere == 45) | |
{ | |
yield break; | |
} | |
if (_iAmAHere == 45) | |
{ | |
yield return 45; | |
} | |
for (var index = start; index < stop; index++) | |
{ | |
yield return index * multiplier; | |
try | |
{ | |
if (index == breakCondition) | |
{ | |
yield break; | |
} | |
if (index == exceptionCondition) | |
{ | |
throw new InvalidOperationException(); | |
} | |
} | |
catch | |
{ | |
Console.WriteLine("Caught"); | |
} | |
finally | |
{ | |
Console.WriteLine("Finally!"); | |
} | |
if (index % 2 == 0) | |
{ | |
yield return 2 * index; | |
} | |
if (index % 3 == 0 && _iAmAHere == 2) | |
{ | |
yield return 3 * index; | |
} | |
yield return 42 * index; | |
} | |
} | |
} |
This file contains hidden or 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.Reflection; | |
using System.Runtime.CompilerServices; | |
using System.Security; | |
using System.Security.Permissions; | |
[assembly: CompilationRelaxations(8)] | |
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] | |
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)] | |
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] | |
[assembly: AssemblyVersion("0.0.0.0")] | |
[module: UnverifiableCode] | |
public class Example | |
{ | |
[CompilerGenerated] | |
private sealed class <DoSomething>d__1 : IEnumerable<int>, IEnumerable, IEnumerator<int>, IDisposable, IEnumerator | |
{ | |
private int <>1__state; | |
private int <>2__current; | |
private int <>l__initialThreadId; | |
public Example <>4__this; | |
private int <start>5__1; | |
private int <stop>5__2; | |
private int <breakCondition>5__3; | |
private int <exceptionCondition>5__4; | |
private int <multiplier>5__5; | |
private int <index>5__6; | |
int IEnumerator<int>.Current | |
{ | |
[DebuggerHidden] | |
get | |
{ | |
return <>2__current; | |
} | |
} | |
object IEnumerator.Current | |
{ | |
[DebuggerHidden] | |
get | |
{ | |
return <>2__current; | |
} | |
} | |
[DebuggerHidden] | |
public <DoSomething>d__1(int <>1__state) | |
{ | |
this.<>1__state = <>1__state; | |
<>l__initialThreadId = Environment.CurrentManagedThreadId; | |
} | |
[DebuggerHidden] | |
void IDisposable.Dispose() | |
{ | |
} | |
private bool MoveNext() | |
{ | |
switch (<>1__state) | |
{ | |
default: | |
return false; | |
case 0: | |
<>1__state = -1; | |
<start>5__1 = 1; | |
<stop>5__2 = 42; | |
<breakCondition>5__3 = 34; | |
<exceptionCondition>5__4 = 41; | |
<multiplier>5__5 = 2; | |
if (<multiplier>5__5 == 2) | |
{ | |
Console.WriteLine("multiplier has been checked"); | |
} | |
if (<>4__this._iAmAHere == 45) | |
{ | |
return false; | |
} | |
if (<>4__this._iAmAHere == 45) | |
{ | |
<>2__current = 45; | |
<>1__state = 1; | |
return true; | |
} | |
goto IL_00d1; | |
case 1: | |
<>1__state = -1; | |
goto IL_00d1; | |
case 2: | |
<>1__state = -1; | |
try | |
{ | |
if (<index>5__6 == <breakCondition>5__3) | |
{ | |
return false; | |
} | |
if (<index>5__6 == <exceptionCondition>5__4) | |
{ | |
throw new InvalidOperationException(); | |
} | |
} | |
catch | |
{ | |
Console.WriteLine("Caught"); | |
} | |
finally | |
{ | |
Console.WriteLine("Finally!"); | |
} | |
if (<index>5__6 % 2 == 0) | |
{ | |
<>2__current = 2 * <index>5__6; | |
<>1__state = 3; | |
return true; | |
} | |
goto IL_0192; | |
case 3: | |
<>1__state = -1; | |
goto IL_0192; | |
case 4: | |
<>1__state = -1; | |
goto IL_01d3; | |
case 5: | |
{ | |
<>1__state = -1; | |
<index>5__6++; | |
break; | |
} | |
IL_01d3: | |
<>2__current = 42 * <index>5__6; | |
<>1__state = 5; | |
return true; | |
IL_0192: | |
if (<index>5__6 % 3 == 0 && <>4__this._iAmAHere == 2) | |
{ | |
<>2__current = 3 * <index>5__6; | |
<>1__state = 4; | |
return true; | |
} | |
goto IL_01d3; | |
IL_00d1: | |
<index>5__6 = <start>5__1; | |
break; | |
} | |
if (<index>5__6 < <stop>5__2) | |
{ | |
<>2__current = <index>5__6 * <multiplier>5__5; | |
<>1__state = 2; | |
return true; | |
} | |
return false; | |
} | |
bool IEnumerator.MoveNext() | |
{ | |
//ILSpy generated this explicit interface implementation from .override directive in MoveNext | |
return this.MoveNext(); | |
} | |
[DebuggerHidden] | |
void IEnumerator.Reset() | |
{ | |
throw new NotSupportedException(); | |
} | |
[DebuggerHidden] | |
IEnumerator<int> IEnumerable<int>.GetEnumerator() | |
{ | |
<DoSomething>d__1 <DoSomething>d__; | |
if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId) | |
{ | |
<>1__state = 0; | |
<DoSomething>d__ = this; | |
} | |
else | |
{ | |
<DoSomething>d__ = new <DoSomething>d__1(0); | |
<DoSomething>d__.<>4__this = <>4__this; | |
} | |
return <DoSomething>d__; | |
} | |
[DebuggerHidden] | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return System.Collections.Generic.IEnumerable<System.Int32>.GetEnumerator(); | |
} | |
} | |
private int _iAmAHere = 0; | |
[IteratorStateMachine(typeof(<DoSomething>d__1))] | |
public IEnumerable<int> DoSomething() | |
{ | |
<DoSomething>d__1 <DoSomething>d__ = new <DoSomething>d__1(-2); | |
<DoSomething>d__.<>4__this = this; | |
return <DoSomething>d__; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment