如果其中一个元素满足要求,如何在 C# 中跳出 foreach
循环?
例如:
foreach(string s in sList){
if(s.equals("ok")){
//jump foreach loop and return true
}
//no item equals to "ok" then return false
}
最佳答案
foreach (string s in sList)
{
if (s.equals("ok"))
return true;
}
return false;
或者,如果您在找到该项目后需要做一些其他事情:
bool found = false;
foreach (string s in sList)
{
if (s.equals("ok"))
{
found = true;
break; // get out of the loop
}
}
// do stuff
return found;
关于c# - 如何跳出 C# 中的 foreach 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6509911/