是的,
goto但这很丑陋,而且并非总是可能的。您还可以将循环放入方法(或匿名方法)中,并使用
return退出返回主代码。
// goto for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { goto Foo; // yeuck! } }Foo: Console.WriteLine("Hi");vs:
// anon-methodAction work = delegate{ for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { return; // exits anon-method } }};work(); // execute anon-methodConsole.WriteLine("Hi");请注意,在C#7中,我们应该获得“本地函数”,(语法tbd等)表示它应该类似于:
// local function (declared **inside** another method)void Work(){ for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { return; // exits local function } }};Work(); // execute local functionConsole.WriteLine("Hi");


