-22

Given the following code snippet:

public class Foo
{
    public IEnumerable<string> Sequence { get; set; }
    public IEnumerable<string> Bar()
    {
        foreach (string s in Sequence)
            yield return s;
    }
}

is the following snippet semantically equivalent, or is it different? If it is different, how do they function differently?

public class Foo2
{
    public IEnumerable<string> Sequence { get; set; }
    public IEnumerable<string> Bar2()
    {
        return Sequence;
    }
}

This question is inspired by this question which is asking a different question about a similar situation.

Community
  • 1
  • 1
Servy
  • 193,745
  • 23
  • 295
  • 406

1 Answers1

26

The two are not equivalent. The semantics of how execution is deferred between the two Bar methods is different. Foo.Bar will evaluate Sequence into an IEnumerable value when you call Bar. Foo2.Bar2 will evaluate Sequence into the value in that variable when you enumerate the sequence returned by Bar2.

We can write a simple enough program to observe the differences here.

//Using iterator block
var foo = new Foo();
foo.Sequence = new[] { "Old" };
var query = foo.Bar();
foo.Sequence = new[] { "New" };
Console.WriteLine(string.Join(" ", query));

//Not using iterator block
var foo2 = new Foo2();
foo2.Sequence = new[] { "Old" };
var query2 = foo2.Bar2();
foo2.Sequence = new[] { "New" };
Console.WriteLine(string.Join(" ", query2));

This prints out:

New
Old

In this particular case our Bar method also has no side effects. If it did it would not be noticeably more important to understand the semantics that your program has, and what it should have. For example, let's modify the two methods so that they have some observable side effects:

public class Foo
{
    public IEnumerable<string> Sequence { get; set; }
    public IEnumerable<string> IteratorBlock()
    {
        Console.WriteLine("I'm iterating Sequence in an iterator block");
        foreach (string s in Sequence)
            yield return s;
    }
    public IEnumerable<string> NoIteratorBlock()
    {
        Console.WriteLine("I'm iterating Sequence without an iterator block");
        return Sequence;
    }
}

Now let's try comparing these two methods to see how they function:

var query = foo.IteratorBlock();
var query2 = foo.NoIteratorBlock();
Console.WriteLine("---");
query.Count();
query.Count();
query2.Count();
query2.Count();

This will print out:

I'm iterating Sequence without an iterator block
---
I'm iterating Sequence in an iterator block
I'm iterating Sequence in an iterator block

Here we can see that the non-iterator block's side effects happen when the method itself is called, and the iterator block's side effects don't happen at that point in time. Then, later on, each time we iterate the non-iterator block it doesn't cause the side effects at all, but the iterator block causes the side effects each time the query is iterated.

Servy
  • 193,745
  • 23
  • 295
  • 406
  • 19
    Congrats, as of [last week](https://stackoverflow.com/help/badges/95/reversal?userid=1159478), you're now the proud holder of [one of two](http://data.stackexchange.com/stackoverflow/query/677715/self-answered-reversals) self-answered Reversal badges on the entire network. – Jason C May 30 '17 at 05:42
  • No need to be rude Jason, it looks like they had a legitimate question and then found an answer. – quantumpotato Feb 12 '18 at 21:15
  • 4
    @quantumpotato He wasn't being rude, just pointing out something they thought was funny (which I also found humorous). Also note that I posted the question and answer at the same time; I knew the answer before I even wrote the question. I only wrote it up because other people demonstrated a misunderstanding of it (in the question linked) so I posted this question to keep the explanation of this topic (which is off topic to that question) from distracting from the question actually being asked there. – Servy Feb 12 '18 at 21:19
  • What about this one? https://stackoverflow.com/help/badges/95/reversal?userid=1749403 – omikes Mar 05 '18 at 02:34
  • @oMiKeY Jason's query didn't find it because it searches for posts that currently meet the criteria for the badge, and that post met the criteria once, but no longer does. – Servy Mar 05 '18 at 14:27
  • 3
    Attempting to understand the logic of the votes is indeed amusing. Good job, answerer, they seem to be saying, for so clearly answering this dumb question. You're clearly much cleverer than that stupid question-asker is. – Mark Amery Oct 04 '18 at 18:13