我想跳过 List<T>.ForEach()
的迭代从 if
中循环陈述。
我有代码:
instructions.ForEach(delegate(Instruction inst)
{
if (!File.Exists(inst.file))
{
continue; // Jump to next iteration
}
Console.WriteLine(inst.file);
});
但是编译器声明没有什么可以跳出的(大概是因为它似乎将 if block 作为封闭 block ?)。
有没有办法做到以上几点?类似于 parentblock.continue;
等
谢谢
最佳答案
使用return
语句代替continue
。请记住,通过使用 ForEach 扩展方法,您正在为每个项目执行一个函数,其主体在 { 和 } 之间指定。通过退出函数,它将继续处理列表中的下一个值。
关于c# - 如何从 C# 中的 if 语句中跳出 List<T>.ForEach() 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11672882/