C#根据foreach中的if语句转到列表中的下一个项目
我正在使用C#。 我有一个项目列表。 我使用foreach循环每个项目。 在我的foreach里面,我有很多if语句来检查一些东西。 如果这些if语句中的任何一个返回false,那么我希望它跳过该项目并转到列表中的下一个项目。 所有if后面的陈述应该被忽略。 我尝试了一个rest,但rest退出整个foreach语句。 
这是我现在有:
 foreach (Item item in myItemsList) { if (item.Name == string.Empty) { // Display error message and move to next item in list. Skip/ignore all validation // that follows beneath } if (item.Weight > 100) { // Display error message and move to next item in list. Skip/ignore all validation // that follows beneath } } 
谢谢
  continue;使用continue; 而不是break; 进入循环的下一次迭代,而不执行任何更多的包含代码。 
 foreach (Item item in myItemsList) { if (item.Name == string.Empty) { // Display error message and move to next item in list. Skip/ignore all validation // that follows beneath continue; } if (item.Weight > 100) { // Display error message and move to next item in list. Skip/ignore all validation // that follows beneath continue; } } 
官方文件在这里 ,但他们不添加很多颜色。
尝试这个:
 foreach (Item item in myItemsList) { if (SkipCondition) continue; // More stuff here } 
你应该使用:
 continue; 
 关键字continue会做你以后的事情。  break会退出foreach循环,所以你会想避免这种情况。 
  continue使用而不是break 。  🙂