C# iterator is executed twice when composing two IEnumerable methods
<p>I just started learning about C# iterator but got confused with the flow of the program after reading the output of the program. The foreach with uniqueVals seems to be executed twice. My understanding is that the first few lines up to the line before "Nums in Square: 3" should not be there. Can anyone help to explain why this happens?</p>

<p>The output is:</p>

<pre>
Unique: 1
Adding to uniqueVals: 1
Unique: 2
Adding to uniqueVals: 2
Unique: 2
Unique: 3
Adding to uniqueVals: 3

Nums in Square: 3
Unique: 1
Adding to uniqueVals: 1
Square: 1
Number returned from Unique: 1
Unique: 2
Adding to uniqueVals: 2
Square: 2
Number returned from Unique: 4
Unique: 2
Unique: 3
Adding to uniqueVals: 3
Square: 3
Number returned from Unique: 9
</pre>

<pre><code>static class Program
    {
      public static IEnumerable&lt;T&gt; Unique&lt;T&gt;(IEnumerable&lt;T&gt; sequence)
        {
            Dictionary&lt;T, T&gt; uniqueVals = new Dictionary&lt;T, T&gt;();

            foreach (T item in sequence)
            {
                Console.WriteLine("Unique: {0}", item);
                if (!uniqueVals.ContainsKey(item))
                {
                    Console.WriteLine("Adding to uniqueVals: {0}", item);
                    uniqueVals.Add(item, item);
                    yield return item;
                    Console.WriteLine("After Unique yield: {0}", item);
                }
            }
        }

        public static IEnumerable&lt;int&gt; Square(IEnumerable&lt;int&gt; nums)
        {
            Console.WriteLine("Nums in Square: {0}", nums.Count());
            foreach (int num in nums)
            {
                Console.WriteLine("Square: {0}", num);
                yield return num * num;
                Console.WriteLine("After Square yield: {0}", num);
            }
        }

        static void Main(string[] args)
        {
            var nums = new int[] { 1, 2, 2, 3 };
            foreach (int num in Square(Unique(nums)))
                Console.WriteLine("Number returned from Unique: {0}", num);
            Console.Read();
        }

    }
</code></pre>
