如何在开关区内摆脱foreach循环?
通常,你使用break但是如果你在switch块中使用break,它只会让你离开switch块而foreach循环将继续执行:
foreach (var v in myCollection)
{
switch (v.id)
{
case 1:
if (true)
{
break;
}
break;
case 2;
break
}
}
Run Code Online (Sandbox Code Playgroud)
当我需要foreach在switch块内打破时,我正在做的是将bool循环外的值设置为true,并在每次foreach输入和进入开关块之前检查此bool的值.像这样的东西:
bool exitLoop;
foreach (var v in myCollection)
{
if (exitLoop) break;
switch (v.id)
{
case 1:
if (true)
{
exitLoop = true;
break;
}
break;
case 2;
break
}
}
Run Code Online (Sandbox Code Playgroud)
这有效,但我一直认为必须有一个更好的方法这样做我不知道...
编辑:想知道为什么这不是在.NET中实现它在PHP中工作的非常简洁的方式@jon_darkstar提到的?
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />\n";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Ree*_*sey 64
在这种情况下,您的解决方案几乎是最常见的选择.话虽这么说,我会在最后进行退出检查:
bool exitLoop;
foreach (var v in myCollection)
{
switch (v.id)
{
case 1:
if (true)
{
exitLoop = true;
}
break;
case 2;
break
}
// This saves an iteration of the foreach...
if (exitLoop) break;
}
Run Code Online (Sandbox Code Playgroud)
另一个主要选项是重构代码,并将switch语句和foreach循环拉出到单独的方法中.然后你可以return从switch语句里面.
sir*_*ide 25
布尔值是单向的.另一个是使用标签和转到.我知道人们认为goto是一个主要的罪恶,但明智地使用(非常明智地),它可能是有用的.在这种情况下,将标签放在foreach循环的末尾.如果要退出循环,只需转到该标签即可.例如:
foreach(var v in myCollection) {
switch(v.Id) {
case 1:
if(true) {
goto end_foreach;
}
break;
case 2:
break;
}
}
end_foreach:
// ... code after the loop
Run Code Online (Sandbox Code Playgroud)
编辑:有些人提到将循环放入一个单独的方法,以便您可以使用返回.我看到了这个的好处,因为它不需要goto,它还简化了包含循环的原始函数.但是,如果循环很简单并且是包含它的函数的主要目的,或者循环使用out或ref变量,那么最好将它保留到位并使用goto.事实上,因为goto和标签脱颖而出,它可能使代码更清晰而不是笨拙.将它放在一个单独的函数中可能会使简单的代码更难阅读.
And*_*zub 17
您可以将foreach循环提取到单独的方法并使用return语句.或者你可以这样做:
foreach (object collectionElement in myCollection)
{
if (ProcessElementAndDetermineIfStop(collectionElement))
{
break;
}
}
private bool ProcessElementAndDetermineIfStop(object collectionElement)
{
switch (v.id)
{
case 1:
return true; // break cycle.
case 2;
return false; // do not break cycle.
}
}
Run Code Online (Sandbox Code Playgroud)
mea*_*gar 10
说实话?这可能是唯一完全有效且适当使用的情况goto:
foreach (var v in myCollection) {
switch (v.id) {
case 1:
if (true)
// document why we're using goto
goto finished;
break;
case 2;
break
}
}
finished: // document why I'm here
Run Code Online (Sandbox Code Playgroud)
它与你的exitLoop旗帜并没有什么不同,但是如果你提取一个方法它可能更具可读性......
foreach (var v in myCollection)
{
if(!DoStuffAndContinue(v))
break;
}
bool DoStuffAndContinue(MyType v)
{
switch (v.id)
{
case 1:
if (ShouldBreakOutOfLoop(v))
{
return false;
}
break;
case 2;
break;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43960 次 |
| 最近记录: |