Skip to content

Incompatability between desktop and coreclr for Generic Array Enumeration #11488

@KevinRansom

Description

@KevinRansom

Enumerator fails to throw when calling current after MoveNext returns false

repro:
This code throws on the NETFramework and the coreclr doesn't

namespace ConsoleApp23
{
    class Program
    {
        static void Main(string[] args)
        {
            var keyValuePairs =
                new KeyValuePair<int, int>[3]
                {
                    new KeyValuePair<int, int>(1,1),
                    new KeyValuePair<int, int>(2,2),
                    new KeyValuePair<int, int>(3,3),
                };

                IEnumerable<KeyValuePair<int, int>> collection = keyValuePairs;

            var e = collection.GetEnumerator();

            var step2 = e.MoveNext();
            var step3 = e.Current;
            Console.WriteLine("{0} {1}", step2, step3);

            var step4 = e.MoveNext();
            var step5 = e.Current;
            Console.WriteLine("{0} {1}", step4, step5);

            var step6 = e.MoveNext();
            var step7 = e.Current;
            Console.WriteLine("{0} {1}", step6, step7);

            var step8 = e.MoveNext();
            var step9 = e.Current;
            Console.WriteLine("{0} {1}", step8, step9);
       }
    }
}

Produces this output on NET Framework: (Note the exception is thrown)

C:\Users\kevinr\source\repos\ConsoleApp23\ConsoleApp23\bin\Debug>net472\ConsoleApp23.exe
True [1, 1]
True [2, 2]
True [3, 3]

Unhandled Exception: System.InvalidOperationException: Enumeration already finished.
   at System.SZArrayHelper.SZGenericArrayEnumerator`1.get_Current()
   at ConsoleApp23.Program.Main(String[] args) in C:\Users\kevinr\source\repos\ConsoleApp23\ConsoleApp23\Program.cs:line 35

C:\Users\kevinr\source\repos\ConsoleApp23\ConsoleApp23\bin\Debug>

and this output on the Coreclr: Note it produces [3,3] when MoveNext returns false

True [1, 1]
True [2, 2]
True [3, 3]
False [3, 3]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions