C# – yield break; – crazy behaviour

.netbreakc++yield

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main (string[] args)
        {
            var test1 = Test1(1, 2);
            var test2 = Test2(3, 4);
        }

        static IEnumerable Test1(int v1, int v2)
        {
            yield break;
        }

        static IEnumerable Test2 (int v1, int v2)
        {
            return new String[] { };
        }
    }
}

"test1" seems to be an IEnumerable with v1 and v2 (params) as fields and "Test1" is NOT called.

"Test2" works a "designed" 🙂

whats going on?

Best Solution

Test1 is called, but unless you iterate through the result, you won't hit a breakpoint on yield break.

Basically Test1 is transformed into a state machine which implements IEnumerable for you... but all of the body of your method is inside that state machine, and unless you use the state machine by calling GetEnumerator() and then MoveNext() (or using a foreach loop) you won't see your body execute.

See my general iterator article and my iterator implementation article for more information, and also two of Eric Lippert's blog posts: Psychic Debugging part one and Psychic Debugging part two.