简体   繁体   English

为什么我不能在匿名方法中的while循环中使用break?

[英]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.

相关问题 为什么我可以使用具有只读自动属性的匿名集合初始化程序,而我不能使用 object 初始化程序 - Why can I use an anonymous collection initializer with a read-only auto-property while I can't use an object initializer 为什么我不能在 C# 中将匿名类型作为泛型返回,而我可以对方法参数执行相同的操作? - why I can't return anonymous type as generic in C#, while I can do the same for method parameters? 为什么这不会在while循环中断? - Why won't this while loop break? 为什么不能将匿名方法分配给 var? - Why can't an anonymous method be assigned to var? 为什么我不能使用这样的匿名方法? - Why I cannot use the anonymous method like that? 为什么不能在C#三元表达式中使用break? - Why can't I use break in a C# ternary expression? 为什么我不能在For循环中使用==? - Why is it that I can't use == in a For loop? 为什么我的匿名方法不能在循环中工作? - Why Doesn't My Anonymous Method Work in a Loop? 为什么我不能在调试器中编辑包含匿名方法的方法? - Why can I not edit a method that contains an anonymous method in the debugger? 我陷入了无限的while循环中,无法理解为什么 - I am stuck in a infinite while loop and can't understand why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM