none
C#7 bug in IEnumerable/IEnumerator. RRS feed

  • Question

  • The following code is taken verbatim from page 292 of C# 6.0 in a Nutshell.  The code compiles and works just fine in C#5/6 (with LINQPad 5). However all hell breaks loose in visual studio with C# 7. The compiler generates numerous errors concerning the explicit/implicit interface definitions of GetEnumerator() and Current(). In C# 7, can the code be modified to work?

    class MyIntList : IEnumerable<int>
        {
            int[] data = { 1, 2, 3 }; // Private OK in C#6. Not OK in C# 7. What gives?
            public IEnumerator<int> GetEnumerator() { return new Enumerator(this); }
            IEnumerator IEnumerable.GetEnumerator() { return new Enumerator(this); } // All hell breaks loose in C# 7.0.

            class Enumerator : IEnumerator<int>
            {
                int index = -1;
                MyIntList collection;

                public Enumerator(MyIntList collection)
                {
                    this.collection = collection;
                }
                public int Current => collection.data[index];   // All hell breaks loose in C# 7.
                object IEnumerator.Current => collection.data[index]; // All hell breaks loose in C# 7.
               

                public bool MoveNext() => ++index < collection.data.Length;
                public void Reset() => index = -1;
                void IDisposable.Dispose() { }
            }        
        }

            

    meouzer

    Wednesday, February 14, 2018 7:17 PM

All replies

  • MeOuzer,

    If there is a 'breaking change' than this forum can not help you (beside with work arounds). 

    You can tell it by doing in Visual Studio -> Help -> FeedBack -> Report a problem 


    Success
    Cor

    Wednesday, February 14, 2018 7:24 PM
  • The code you posted compiles just fine in C# 7 as it would have in previous versions. Please post the compiler errors you're getting along with the full file contents. The only thing I can think of that you might have forgotten to do is add a using for System.Collections as that is not a namespace that is generally included by default anymore but it is needed to get back to the legacy IEnumerable implementation.

    using System;
    using System.Collections;
    using System.Collections.Generic;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main ( string[] args )
            {
                var instance = new MyIntList();
    
                foreach (var value in instance)
                    Console.WriteLine(value);
            }
        }
    
        class MyIntList : IEnumerable<int>
        {
            int[] data = { 1, 2, 3 }; // Private OK in C#6. Not OK in C# 7. What gives?
            public IEnumerator<int> GetEnumerator () { return new Enumerator(this); }
            IEnumerator IEnumerable.GetEnumerator () { return new Enumerator(this); } // All hell breaks loose in C# 7.0.
    
            class Enumerator : IEnumerator<int>
            {
                int index = -1;
                MyIntList collection;
    
                public Enumerator ( MyIntList collection )
                {
                    this.collection = collection;
                }
                public int Current => collection.data[index];   // All hell breaks loose in C# 7.
                object IEnumerator.Current => collection.data[index]; // All hell breaks loose in C# 7.
    
                public bool MoveNext () => ++index < collection.data.Length;
                public void Reset () => index = -1;
                void IDisposable.Dispose () { }
            }
        }
    }
    


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, February 15, 2018 4:13 PM
    Moderator