What is if if you look in IL?

What does a statement look like ifwhen it is compiled into IL?

This is a very simple construction in C #. Can someone give me a more abstract definition of what it really is?

+3
source share
4 answers

Here are a few statements ifand how they switch to IL:

ldc.i4.s 0x2f                      var i = 47;
stloc.0 

ldloc.0                            if (i == 47)
ldc.i4.s 0x2f
bne.un.s L_0012

ldstr "forty-seven!"                   Console.WriteLine("forty-seven!");
call Console::WriteLine

L_0012:
ldloc.0                            if (i > 0)
ldc.i4.0 
ble.s L_0020

ldstr "greater than zero!"             Console.WriteLine("greater than zero!");
call Console::WriteLine

L_0020:
ldloc.0                            bool b = (i != 0);
ldc.i4.0 
ceq 
ldc.i4.0 
ceq 
stloc.1 

ldloc.1                            if (b)
brfalse.s L_0035

ldstr "boolean true!"                  Console.WriteLine("boolean true!");
call Console::WriteLine

L_0035:
ret

One thing to note: IL instructions are always "opposite." if (i > 0)converts to something that effectively means "if i <= 0, and then jumps over the block body if."

+8
source

, () .

brfalse Branch to target if value is zero (false)
brtrue  Branch to target if value is non-zero (true)
beq     Branch to target if equal
bge     Branch to target if greater than or equal to
bgt     Branch to target if greater than
ble     Branch to target if less than or equal to
blt     Branch to target if less than
bne.un  Branch to target if unequal or unordered
+4

if. , null, brfalse ( brtrue , ).

if , , ILDASM Reflector, .

+3

:

ldloc.1                    // loads first local variable to stack
ldc.i4.0                   // loads constant 0 to stack
beq                        // branch if equal

if(i == 0) //if i is the first local variable

ifs , . , IL-Code.

codeproject.

+1

Source: https://habr.com/ru/post/1763689/


All Articles