2

I don't really need this feature, but an opportunity to simplify some code has presented itself if I could get the compiler to permit yield return with another IEnumerable<>. Example shown below:

static void Main(string[] args){
    foreach (string str in EnumerateA())
        Console.WriteLine(str);
}
static IEnumerable<string> EnumerateA(){
    yield return "1";
    yield return EnumerateB("2");
    yield return EnumerateB("3");
}
static IEnumerable<string> EnumerateB(string num){
    yield return num + "+";
    yield return num + "-";
}

I know that I can replace

yield return EnumerateB("2")

with

foreach(string str in EnumerateB("2")) yield return str;

and have that work, but is that the only way this would work? I'm targeting .NET 2.0.

1
  • Yes................................ Commented Aug 28, 2013 at 4:30

2 Answers 2

3

Yes, that is the only way it would work. Each value should be returned using the 'yield return', you cannot return values as group of collection.

This is wrong

static IEnumerable<string> EnumerateA()
{
    yield return "1";
    yield return EnumerateB("2");
    yield return EnumerateB("3");
}

Correct way would be

static IEnumerable<string> EnumerateA()
{
    yield return "1";
    foreach (var str in EnumerateB("2"))
    {
        yield return str;
    }
    foreach (var str in EnumerateB("3"))
    {
        yield return str;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It would appear that if I was using .NET 3.5+ I could use Enumerable.Concat<TSource> to return a concatenation of those IEnumerable<string>, and if I was F# I could use yield!. As I'm using neither, I think you might be right.
1

As a fairly simple alternative you could implement your code using normal static methods.

Just define these:

public static class Enumerable
{
    public static IEnumerable<T> Return<T>(T value)
    {
        yield return value;
    }

    public static IEnumerable<T> Concat<T>(params IEnumerable<T>[] lists)
    {
        foreach (var values in lists)
            foreach (var value in values)
                yield return value;
    }
}

Then implement your method like this:

static IEnumerable<string> EnumerateA()
{
    return
        Enumerable.Concat(
            Enumerable.Return("1"),
            EnumerateB("2"),
            EnumerateB("3"));
}

I know it is not as nice as using static methods that given a good fluent architecture, but it's not too bad I think. It will also help if, in the future, you get to upgrade your code. If you follow the linq signatures the code may even still run with few changes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.