在foreach循环中开关巢使用`continue` keywoard [英] Using `continue` keywoard in a switch nest inside a foreach loop

查看:176
本文介绍了在foreach循环中开关巢使用`continue` keywoard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的代码(实际上是比你看到更长的时间!)

I have the code below (which actually is much longer than you see!)

foreach (SensorPair sensor in _sensorPairs)
{
    sensorByte = (byte) sensor.Sensor;

    if (!packet.Contains(sensorByte))
        continue;

    index = packet.IndexOf(sensorByte);
    byteCount = sensor.ByteCount;

    switch (byteCount)
    {
        case 1:
            try
            {
                switch(sensor.ValueType)
                {
                    case SensorValueType.Unsigned:
                        val = (int)packet[index + 1];
                        if (val > 255)
                            //*** WHAT DOES THIS CONTINUE DO?
                            continue;   
                        else //rise the event
                           OnSensorReport();                              
                    break;



请问继续 keywoard你看到引起foreach循环开始itterating下一个项目或者它只是传递到下一个情况语句?

Does the continuekeywoard you see cause the foreach loop to start itterating next item or it just passes to the next case statement?

如果它不这样做与foreach循环什么,我怎么能强制代码退出开关,并开始在foreach循环中的下一个itteration?

If it does not do anything with the foreach loop, how can I force the code to exit the switch and starts the next itteration in the foreach loop?

推荐答案

是的,它延续了的foreach 循环。

它总是有用咨询的 文档 ; - )

It is always useful to consult the documentation ;-)

continue语句将控制权交给
的下一个迭代封闭的同时,做了,或者它出现foreach语句。

The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears.

或MDASH;更全面和mdash;以下 C#语言规范

or—more comprehensive—the C# language specification:

8.9.2 continue语句

continue语句开始在最近的封闭
而新的迭代,做,for或foreach语句

The continue statement starts a new iteration of the nearest enclosing while, do, for, or foreach statement.

continue语句的目标是直接封闭,而嵌入式
语句的结束点,做,for或foreach
语句。如果continue语句不是由而封闭的,做的,
for或foreach语句,则发生编译时错误。

The target of a continue statement is the end point of the embedded statement of the nearest enclosing while, do, for, or foreach statement. If a continue statement is not enclosed by a while, do, for, or foreach statement, a compile-time error occurs.

在多个,同时,做,for或foreach语句是嵌套
内对方,continue语句适用的只有最里面的
语句
。要穿越多个嵌套层转移控制,一个goto
语句(第8.9.3节)必须使用。

When multiple while, do, for, or foreach statements are nested within each other, a continue statement applies only to the innermost statement. To transfer control across multiple nesting levels, a goto statement (§8.9.3) must be used.

continue语句不能退出finally块(§ 8.10)。当
继续内的最后出现语句块中,
的目标continue语句必须在同一finally块;否则出现
编译时错误

A continue statement cannot exit a finally block (§8.10). When a continue statement occurs within a finally block, the target of the continue statement must be within the same finally block; otherwise a compile-time error occurs.

语句按如下规则执行的继续:

A continue statement is executed as follows:


  • 如果continue语句退出关联finally块与
    的一个或多个try块,则控制最初转移到
    最后最里面的try语句块。当(如果)控制
    到达finally块的结束点时,控制转移到
    下一个封闭try语句的finally块。这个过程是
    重复进行,直到所有的try语句
    的finally块都被执行。

  • If the continue statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed.

控制转移到目标在继续
语句。

Control is transferred to the target of the continue statement.

由于continue语句无条件地将控制
别处,最终continue语句的一点是永远无法到达。

Because a continue statement unconditionally transfers control elsewhere, the end point of a continue statement is never reachable.

这篇关于在foreach循环中开关巢使用`continue` keywoard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆