As I was going through the various control flow statements in C# I came across the goto statement that works the same way as VB6 did. Here is a sample.
using System;
public class GotoTest
{
static void Main()
{
int i;
for (i = 0; i < 10; i++)
if (i == 5)
goto Reached5;
Reached5:
Console.WriteLine("i = {0}",i);
}
}
It can also be used to transfer control to a specific switch-case label but knowing that Goto has the potential to create spaghetti code I wonder why the C# team allowed it. It was understandable in VB .NET due to backward compatibility but developers are discouraged to use it for new development. The C# documentation states:
The goto statement is also useful to get out of deeply nested loops.
Hmm... do we not practice what we preach?