c# - 语法:yield return enumerable without loop

标签 c# syntax

这可能吗?

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/

相关文章:

c# - Entity Framework ,比较复杂类型

c# - 从 USB PC Remote 读取输入

.net - 使用对象初始化语法的操作顺序

objective-c - 在 Obj-C 中使用 C 语法调用静态方法?

c# - 用于深度属性比较的表达式生成器

c# - 在 C# 中禁用键盘/鼠标

c# - 如何列出相机可用的视频分辨率

c++ - 无法将虚函数移到 header 之外

c++ - 为什么 for(class A{} fkldsjflksdjflsj;;) 可以编译?

c++ - "({ expr1; expr2; })"是如何用C++编译的?