[英]Why can't I use break in a while loop in an anonymous method?
Why can't I use a break;
我为什么不能
break;
statement in a while
loop, whilst in an anonymous method? 在
while
循环中声明,而在匿名方法中?
I was working on the piece of code (below), when I got this error: " Control cannot leave the body of an anonymous method or lambda expression
". 当出现以下错误时,我正在处理一段代码(如下):“
Control cannot leave the body of an anonymous method or lambda expression
”。
Thankfully I can solve the problem by using return;
值得庆幸的是,我可以使用
return;
来解决问题return;
instead, but I'd still like to know why I can't use break;
相反,但是我仍然想知道为什么我不能使用
break;
. 。 To me, the main difference between the two statements, was that
return;
对我来说,这两种说法的主要区别在于
return;
exits a method, and break;
退出方法并
break;
exits the further-most nested loop. 退出最嵌套的循环。
My code, 我的代码
while (someCondition)
{
System.Threading.Thread.Sleep(500);
Application.Current.Dispatcher.Invoke(new Action(() =>
{
if (someOtherCondition)
{
// Do stuff...
}
else
{
if (anotherCondition)
{
break;
}
// Do something else...
}
}));
}
Rewriting the code helps to explain why: 重写代码有助于解释原因:
while (someCondition)
{
System.Threading.Thread.Sleep(500);
Application.Current.Dispatcher.Invoke(MyMethod);
}
private void MyMethod()
{
if (someOtherCondition)
{
// Do stuff...
}
else
{
if (anotherCondition)
{
break;
}
// Do something else...
}
}
You are breaking inside a function that has no loop. 您正在破坏一个没有循环的函数。 The loop exists in another method.
循环存在于另一种方法中。 So return needs to be called instead, as you found out.
如您所知,因此需要调用return。 Just because you are using an annonymous method, it's still a separate method to the one containing the while loop.
仅仅因为您使用的是匿名方法,它仍然是与包含while循环的方法不同的方法。
For the same reason you can't do this: 出于同样的原因,您不能执行以下操作:
while(1)
{
method1();
}
void method1()
{
break;
}
Even if your anonymous method is written in the same function as your while loop, it can still be called from somewhere where there isn't a while loop around. 即使您的匿名方法是使用与while循环相同的函数编写的,也仍然可以从没有while循环的地方调用它。
break; 打破; cannot be used to exit methods, instead you need a return.
不能用于退出方法,而需要返回。 And while inside a method your scope is limited to that method because it could have been called from anywhere.
而在方法内部,您的作用域仅限于该方法,因为它可以从任何地方调用。 While inside the method there is no information on the calling scope and the code therefore does not know if there is a loop to break out of.
在方法内部时,没有有关调用范围的信息,因此代码不知道是否存在要中断的循环。 So a method scope is different than the scope of an if-statement.
因此,方法范围不同于if语句的范围。
Return takes you out of a method or a function but break gets you out of the loop or an iteration. Return使您脱离方法或函数,而break使您脱离循环或迭代。
This is the main diff. 这是主要区别。
// This is Oki
public void Do()
{
for (int i = 0; i < 10; i++)
{
break;
}
}
// This is a compiler error
public void Do()
{
break;
}
You can change condition of while: 您可以更改while条件:
while (someCondition)
{
System.Threading.Thread.Sleep(500);
Application.Current.Dispatcher.Invoke(new Action(() =>
{
if (someOtherCondition)
{
// Do stuff...
}
else
{
if (anotherCondition)
{
//break;
someCondition = false;
return;
}
// Do something else...
}
}));
if (!someCondition)
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.