我有下面的代码(实际上比您看到的要长得多!)
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循环中开始下一次迭代?
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;Does the continuekeywoard you see cause the foreach loop to start itterating next item or it just passes to the next case statement?
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循环。
咨询文档总是有用的;-)
continue语句将控制传递给下一个包含while,do,for或foreach语句的迭代。
或更全面的C#语言规范 :
8.9.2继续声明
continue语句为最近圈出的while,do,for或foreach语句开始新的迭代。
continue语句的目标是最接近的while,do,for或foreach语句的嵌入语句的终点。 如果continue语句没有被while,do,for或foreach语句包含,则会发生编译时错误。
当多个while,do,for或foreach语句彼此嵌套时,continue语句仅适用于最内层语句 。 为了跨多个嵌套层次传输控制,必须使用goto语句(第8.9.3节)。
continue语句不能退出finally块(第8.10节)。 当continue语句在finally块内发生时,continue语句的目标必须位于finally块内; 否则会发生编译时错误。
continue语句执行如下:
如果continue语句退出一个或多个带有关联的finally块的try块,则控制最初会转移到最内层try语句的finally块中。 当和如果控制到达finally块的结束点时,控制权将转移到下一个包含try语句的finally块中。 重复这个过程,直到所有中间try语句的finally块都被执行完毕。
控制权转移到continue语句的目标。
因为continue语句无条件地将控制权转移到其他地方,所以continue语句的终点永远不可达。
Yes, it continues the foreach loop.
It is always useful to consult the documentation ;-)
The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears.
or—more comprehensive—the C# language specification:
8.9.2 The continue statement
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.
更多推荐
发布评论