c# - 如何生成嵌套的 IENumerable?

标签 c# methods yield

我知道如何在具有返回值的方法中生成值 IENumerable

public IEnumerable<int> GetDigits()
{
    yield return 1;
    yield return 1;
    yield return 1;
}

但是嵌套 IEnumerable<IEnumerable<int>> 的正确语法是怎样的呢? ?

public IEnumerable<IEnumerable<int>> GetNestedDigits()
{
    yield return yield return 1; //??
}

最佳答案

您不能直接嵌套yield return 语句。您必须创建另一种方法:

public IEnumerable<IEnumerable<int>> GetNestedDigits()
{
    yield return GetNestedEnumerable();
}

public IEnumerable<int> GetNestedEnumerable()
{
    yield return 1;
}

关于c# - 如何生成嵌套的 IENumerable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39450044/

相关文章:

c# - asp.net 网页中 Request 和 Request.Form 的良好实践 2

magento - 如何在 Magento 的总计框中获取运输方式输出

python - 带 yield 的递归函数不返回任何东西

python - 在另一个函数中为生成器调用 yield

c# - 在 C# 中为 jpg 文件加水印

c# - WCF - 检测响应中的错误

scala - 在Scala中,父特征是否可以调用子类中实现的方法?

ruby - 如何将Regexp.last_match传递给Ruby中的 block

c# - 如何获取 XML 属性的值?

php - 在 PHP 链中使用变量作为方法以允许条件方法链接