I get this error message and I can’t figure out why!
Error 1 'Exo5Chap12.ShortCollection<T>' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'Exo5Chap12.ShortCollection<T>.GetEnumerator()' cannot implement
'System.Collections.IEnumerable.GetEnumerator()' because it does not have the matching
return type of 'System.Collections.IEnumerator'.
E:\MyFolders\Dev\c#\Chapter12\Exo5Chap12\Exo5Chap12\exo5.cs 9 18 Exo5Chap12
Here is the code with an implementation of GetEnumerator().
What is wrong?
public class ShortCollection<T> : IList<T>
{
protected Collection<T> innerCollection;
protected int maxSize = 10;
public IEnumerator<T> GetEnumerator()
{
return (innerCollection as IEnumerator<T>).GetEnumerator();
}
}
IEnumerableandIEnumerable<T>are different interfaces and your code has to implement both. Your code only implements the GetEnumerator() ofIEnumerable<T>, but not GetEnumerator() ofIEnumerable. You should consider installing ReSharper which makes it easy to fix errors like this one.