C# - 使用if语句的Foreach循环

时间:2012-06-19 23:26:44

标签: c# if-statement foreach

如果“if”语句为真,我怎么能这样做,跳过foreach循环下面的代码,继续执行程序的其余部分

void()
{
    foreach()
    {
        if()
        {

        }
    }

    //code I want to skip if "if" statement is true

}

8 个答案:

答案 0 :(得分:4)

没有办法直接做你想要的东西(没有“转到”标签 - 消灭了思想!),但你可以使用“break”关键字,并设置一个你可以在以后参考的变量。

void()
{
    var testWasTrue = false;
    foreach()
    {
        if()
        {
            testWasTrue = true;
            break;  // break out of the "foreach"
        }
    }

    if( !testWasTrue ) {
        //code I want to skip if "if" statement is true
    }

}

答案 1 :(得分:3)

我知道这已经得到了解答,但我认为我投入了2美分,因为没有人考虑将支票抽象为单独的方法:

void()
{
    if (ShouldDoStuff(myCollection))
        DoStuff(myCollection);
    else
        DoOtherStuff(myCollection);
}

private bool ShouldDoStuff(collection)
{
    foreach()
    {
        if ()
            return true;
    }
    return false;
}

这为处理您的算法提供了更高级别的更清晰的代码,并消除了所讨论的所有混乱。它干净地分离了检查和执行操作的void()中的任务,读者立即确切地知道程序流程是什么,而不必通过布尔或破坏逻辑来辨别他们正在做什么。没有一种方法可以承担超过1项责任或任务。

是的,海报可能希望在他们的foreach中做其他工作,但这是一个完全不同的讨论,而不是他们的问题中描述的内容。如果您只想检查给定集合(或对象)是否满足某个条件,则可以将该检查移动到单独的方法。甚至将门打开以进行所有三个组件的自动化单元测试。

即使DoStuffDoOtherStuff没有抽象到自己的方法,它也提供了更好的可读性和逻辑流程。

答案 2 :(得分:1)

'break'关键字将脱离循环。

foreach (someClass a in someArray) 
{
  if(a.someProperty) // bool property 
  {
    //Stuff to do if that condition is true
    doSomethingElse();
    //Calling the break keyword will stop the loop and jump immediately outside of it
    break;
  }
  //Other code to run for each iteration of the loop
}

//Here is where execution will pick up either after break is called or after the loop finishes

答案 3 :(得分:1)

void()
{
     bool process = true;
     foreach()
     {
          if()
          {
              process = false;
              break;
          }
     }

     if (process)
     {
       //code I want to skip if "if" statement is true
     }

}

答案 4 :(得分:1)

正如我在评论中提到的,你可以通过额外的bool变量来做到这一点。

void()
    {
        bool positiveResult; // by default it gets false value
        foreach()
        {
            if()
            {
                positiveResult = true;
                // you may use "break" to skip the loop
                break;
            }
        }

        if( !positiveResult  ) 
         {
            //code I want to skip if "if" statement is true
         }

    }

答案 5 :(得分:0)

void() 
{ 
    bool skip = false;
    foreach() 
    { 
        if() 
        { 
           skip = true;
        } 
    } 

    if(!skip)
    {
        //code I want to skip if "if" statement is true 
    }
} 

答案 6 :(得分:0)

我只知道bool旗是怎么回事。

void()
{
  bool x = false;
  foreach()
  {
    if()
    {
      x = true;
      break;
    }
  }
  if(!x)
  {
    //Code to skip if "if" statement is true.
  }
}

不是超级优雅,但很容易。 编辑:12秒击败:)

答案 7 :(得分:0)

如果要迭代的集合包含IEnumerable接口,则可以将Lambda与Any()一起使用!

int[] myArray = { 1, 2, 3 };

                if( myArray.Any((a) => a == 1) )
                {
                    return;
                }

读取:如果我的数组包含a等于1的任何值a,则返回此函数。

此外,如果您想使其更难以阅读,则可以省略花括号/括号。

if( myArray.Any((a) => a == 1) )
     return;
相关问题