If you are forced to simplify C# looping keywords, choose only one you want to preserve [closed]
If you are forced to simplify C# keywords that can be used for looping, choose only one you want to preserve.
- for
- do-while
- while
- goto-label-if
- foreach
Is there any performance consideration regarding your decision?
Actually I don't know the internal mechanism of them, so here开发者_JS百科 I want interview those of you know the details. However, someone already closed it. So sad!
goto-label-if
is not actually looping. And you missed foreach
.
CS theory states, that you only need while
to express everything else (even conditional statements), so I'll preserve it. If you were to invent truly minimal imperative programming language, subroutine calls and while
loop will suffice.
I would keep goto-label-if
. That is what the compiler turns everything into anyway. The most basic form of flow control is conditional branching and that is done with the branch
/jump
opcodes.
I have examples of loop conversions on the answer to another question.
... this C# code ...
static void @ifgoto(bool input)
{
label:
if (input)
goto label;
}
static void @while(bool input)
{
while (input) ;
}
static void @for(bool input)
{
for (; input; ) ;
}
... Compiles to this ...
.method private hidebysig static void ifgoto(bool input) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: brtrue.s L_0000
L_0003: ret
}
.method private hidebysig static void while(bool input) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: brtrue.s L_0000
L_0003: ret
}
.method private hidebysig static void for(bool input) cil managed
{
.maxstack 8
L_0000: ldarg.0
L_0001: brtrue.s L_0000
L_0003: ret
}
.. To explain this more ...
// load input
L_0000: ldarg.0
// if input is true branch to L_000
L_0001: brtrue.s L_0000
// else return
L_0003: ret
While. Everything else can be emulated in a while loop.
I'd be sad, because i love my for loops :-(
I would leave for
loop - you can omit some part of this loop and simulate other loops. And in the same time you will get more powerful loop if you'll use all parts.
精彩评论