I´m a big fan of neat constructs such as the yield return in C#.

The yield return allows you to return results from a method line by line instead of summing it up in a list or such. You can see the difference between these two methods:

public IEnumerable<string> ReturnedAsList () {
    return new List<string> { "Str1", "Str2" };
}

public IEnumerable<string> ReturnedWithYield () {
    yield return "Str1";
    yield return "Str2";
}

You can easily see how this makes returning from iterations and recursive operations really neat and tidy. However, you need to be really really careful with this as I just experienced. The IEnumerable does more than you bargain for, it doesn’t actually evaluate the expression when you call the method but waits until it’s processed. So, how does this manifest?

Look at this test, the comment shows what you´d expect to be written as a result message:

private string Result = String.Empty;

[TestMethod]
public void TestMethod1 () {
    var result = GetResults ();

    Result += "Before writing result";

    Assert.Fail (Result + "rn" + string.Join ("rn", result));

    // Expected:
    // Starting
    // Returned 1
    // Returned 2
    // Completed
    // Before writing result
    // Test 1
    // Test 2
    // Same as above again...
}

public IEnumerable<string> GetResults () {
    Result += "Starting";

    yield return "Test 1";
    Result += "Returned 1";

    yield return "Test 2";
    Result += "Returned 2";

    Result += "Completed";
}

But what about the actual result? It looks like this:

// Before writing result

// Test 1

// Test 2

This is due to the results of the enumerable method not being well, enumerated, until its actually processed. So, when adding Result-string it only contains the first text and after we´ve enumerated in the method the added textrows havent been used in the string concatenation.

Neat construct but aye dangerous for the unwitful mind says I, you really need to keep an eye out for these lest you end up like me 🙂

Why not join the newsletter?

Why not join the newsletter?

If you're not a fan of facebook or twitter, sign up to the newsletter and I'll send you a quick update every so often with what's going on and new posts coming out.

You have Successfully Subscribed!