Trouble understanding yield in C# : <p>I'm hoping to get some clarification on a snippet that I've recently stepped through in the debugger, but simply cannot really understand.</p>

<p>I'm taking a <strong>C#</strong> course on <strong>PluralSight</strong> and the current topic is on <code>yield</code> and returning a <code>IEnumerable&lt;T&gt;</code> with the keyword. </p>

<p>I've got this overly basic function that returns an <code>IEnumerable</code> collection of <code>Vendors</code> (A simple class with <code>Id</code>, <code>CompanyName</code> and <code>Email</code>):</p>

<pre><code>public IEnumerable&lt;Vendor&gt; RetrieveWithIterator()
{
    this.Retrieve(); // &lt;-- I've got a breakpoint here
    foreach(var vendor in _vendors)
    {
        Debug.WriteLine($"Vendor Id: {vendor.VendorId}");
        yield return vendor;
    }
}
</code></pre>

<p>And I've got this code in a unit test that I'm using to test the function:</p>

<pre><code>var vendorIterator = repository.RetrieveWithIterator(); // &lt;-- Why don't it enter function?
foreach (var item in vendorIterator) // &lt;-- But starts here?
{
    Debug.WriteLine(item);
}
var actual = vendorIterator.ToList();
</code></pre>

<p>What I really can't seem to understand, and I'm sure a lot of beginners are having the same trouble, is why the initial call to <code>RetrieveWithIterator</code> doesn't initiate the function, but it rather starts when we start iterating through its returned <code>IEnumerable</code> collection (see the comments).</p>
