An example of an infinite loop with minimal code in C #

Can you give me an example of an infinite loop in C # with minimal code? I came up with something, but I thought there might be an easier way.

+3
source share
8 answers

Typical examples are for and while loops. for instance

for(;;)
{}

and

while(true)
{}

However, basically any cyclic construction without interruption or termination condition will continue indefinitely. Different developers have different opinions about which style is better. In addition, the context may affect which method you choose.

+22
source
while (true);

That should be enough.

Generated IL:

IL_0000:  br.s        IL_0000

The code unconditionally transfers control to itself. A great way to avoid processor cycles.

+11

:

while (true)
{
    // do stuff
}

:

while (true)
{
    if (condition)
        break;
}
+9

, , :

for (;;) { }

l: goto l;
+6

Code Golf:

for(;;);
+3

, WAY CPU.:)

System.Threading.Thread.Sleep(-1);
+3
source

Try this, an example of an infinite loop.

while(true)
{

}
0
source

Call the method in the same method, and you have an infinite loop (only conditions make you break the loop)

void HelloWorld()
{
   HelloWorld();
}
-3
source

All Articles