This is a mirror of official site: http://jasper-net.blogspot.com/

Internals of loops (While, For and ForEach)

| Sunday, January 23, 2011
The Internals

Before going further with the internals of loop let me clear out one fact which you always remember. Goto is a construct(keyword) in C# that allows you to unconditionally transfer the control from one place to another. The IL equivalent for goto statement is br.s which takes just an instruction line number to transfer the control to a place. Another small instruction brtrue.s which evaluates the loaded object and moves the control to the instruction line only when the evaluated value turns out to be true.

Hence to summarize :
br.s -> Unconditionally moves the control.
brtrue.s -> Moves the control if loaded value is true.

Now lets start looking at the internals one by one :


While, do - while Loop :
int x=10;
while (x >= 0)
{
}

If you look into the IL instruction set it creates an integer variable x (which we have declared in code) and a placeholder for boolean variable. The L_0004 instruction is actually a goto equivalent which moves the control over to the location specified. It evaluates and stores the result of equality of x with 0; clt and  ceq specifies the less than and equal respectively.
Based on the result,  L_0011 is evaluated and control moves to specified instruction line accordingly.

Read more: DOT NET TRICKS

Posted via email from Jasper-net

0 comments: