Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to yield a nested IENumerable?

Tags:

yield

methods

c#

I know how to yield values in a method with return value IENumerable

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

but how is the correct Syntax for a nested IEnumerable<IEnumerable<int>>?

public IEnumerable<IEnumerable<int>> GetNestedDigits()
{
    yield return yield return 1; //??
}
like image 283
c0rd Avatar asked Oct 19 '25 14:10

c0rd


1 Answers

You cannot directly nest the yield return statements. You'd have to create another method:

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

public IEnumerable<int> GetNestedEnumerable()
{
    yield return 1;
}
like image 158
Kenneth Avatar answered Oct 22 '25 04:10

Kenneth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!