Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

infinite loop example with minimum code in c#

Can you give me an infinite loop example on c# with minimum code? I came up with something but I thought there could be more easier way.

like image 348
tugberk Avatar asked Nov 27 '22 21:11

tugberk


2 Answers

The typical examples are the for and while loops. For example

for(;;)
{}

and

while(true)
{}

However, basically any looping construct without a break or a terminating condition will loop infinitely. Different developers have different opinions on which style is best. Additionally, context may sway which method you choose.

like image 123
mdec Avatar answered Dec 05 '22 04:12

mdec


while (true);

That should be enough.

The generated IL is:

IL_0000:  br.s        IL_0000

The code unconditionally transfers control to itself. A great way to waste CPU cycles.

like image 20
Martin Liversage Avatar answered Dec 05 '22 04:12

Martin Liversage