这可能吗?
List<Cat> _Cats; // created somewhere else
public IEnumerable<Cat> LeadWithLolCat()
{
yield return RandomLolCat();
// can we return all the other cats without creating a loop?
foreach ( var cat in _Cats ) // -> so not like this
yield return cat;
}
最佳答案
您可以像这样尝试使用 Concat
:
public IEnumerable<Cat> LeadWithLolCat()
{
return new [] { RandomLolCat() }.Concat(_Cats);
}
关于c# - 语法:yield return enumerable without loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25192146/