Hello, the question is simple, what is wrong in the following code:

public class some_class : IEnumerable<another_class> { private another_class[] class_list; IEnumerator<another_class> IEnumerable<another_class>.GetEnumerator() { return (IEnumerator<another_class>)class_list.GetEnumerator(); } ... } 

The compiler persistently gives the error: ... the member of the interface "System.Collections.IEnumerable.GetEnumerator ()" is not implemented

It's not entirely clear how you can implement the IEnumerable.GetEnumerator () method, because IEnumerable is a generic interface and you need to specify the type of the collection (which I did). Thank you in advance.

  • Understand the fact of the matter: I tried to implement the 2nd method, but the compiler immediately underlines the word IEnumerator in red, if you do not specify the type T. Probably it will be clearer to show a specific piece of code: public class garage: IEnumerable <Sport_car> {private Sport_car [] car_list; IEnumerator <Sport_car> IEnumerable <Sport_car> .GetEnumerator () {return (IEnumerator <Sport_car>) car_list.GetEnumerator (); } IEnumerator IEnumerable.GetEnumerator () {return car_list.GetEnumerator (); } .. - nvse
  • Writes that using the universal type "System.Collection.Generic.IEnumerator <T>" requires an argument of type "1" That is, you can't even just write IEnumerator for some reason - nvse
  • Correct IEnumerator on System.Collections.IEnumerator , or add the appropriate using . Inumerator and IEnumerator<T> are different types, they are in different namespaces, but the second interface requires the implementation of the first. - AlexeyM

1 answer 1

The interface that you implement in turn is implemented by the non-typed IEnumerable:

 public interface IEnumerable<out T> : IEnumerable { IEnumerator<T> GetEnumerator(); } public interface IEnumerator<out T> : IDisposable, IEnumerator { T Current { get; } } public interface IEnumerator { object Current { get; } bool MoveNext(); void Reset(); } 

Therefore, you need to implement its "insides", namely, another GetEnumerator () method and IEnumerator <out T>:

 using System.Collections; using System.Collections.Generic; public class GarageEnumerator<T> : IEnumerator<T> { private readonly IEnumerator enumerator; public GarageEnumerator(IEnumerator enumerator) { this.enumerator = enumerator; } public void Dispose() { } public bool MoveNext() { return enumerator.MoveNext(); } public void Reset() { enumerator.Reset(); } public T Current { get { return (T)enumerator.Current; } } object IEnumerator.Current { get { return Current; } } } public class Garage<T> : IEnumerable<T> { private readonly T[] carList; //initialization ! public IEnumerator<T> GetEnumerator() { return new GarageEnumerator<T>(carList.GetEnumerator()); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } 

In general, use the built-in tools in VS - it can do it for you.

UPDATE: from 3.5 framework you can connect:

 using System.Linq; 

and implementation:

 public class Garage<T> : IEnumerable<T> { private readonly T[] carList = new T[10]; //initialization ! public IEnumerator<T> GetEnumerator() { return carList.Cast<T>().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }