c# - 在 foreach 循环内的开关嵌套中使用 `continue` 关键字

标签 c# loops foreach switch-statement continue

<分区>

我有下面的代码(实际上比你看到的要长得多!)

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;

您看到的 continue 关键字是否导致 foreach 循环开始迭代下一个项目,或者它只是传递到下一个 case 语句?

如果它不对 foreach 循环做任何事情,我如何强制代码退出开关并开始 foreach 循环中的下一次迭代?

最佳答案

是的,它继续 foreach 循环。

咨询documentation总是有用的;-)

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

或者——更全面——C# language specification :

8.9.2 continue 语句

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

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.

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.

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:

  • 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.

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

关于c# - 在 foreach 循环内的开关嵌套中使用 `continue` 关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14204992/

相关文章:

amazon-web-services - Terraform 因无效的 for_each 参数而失败/给定的 "for_each"参数值不合适

java - foreach 变量在编译时导致 "<identifier> expected"错误

node.js - foreach 在react-redux 中如何工作

c# - 构造函数中的代码是否添加到子类构造函数中的代码?

c# - 如何清除表单中所有textBoxes的文字?

c# - 窗体最大化时的中心控件

java - .hasNext() 上的无限循环

c# - OpenXML 从 Excel 创建数据表 - 货币单元格值不正确

c - 我的c程序没有给出任何结果。请帮助大家

php - 如何使用 PHP 计算动态 HTML 表单输入数组?