Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error message regarding IEnumerable.GetEnumerator()

Tags:

c#

ienumerable

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();
    }
}
like image 598
Bon_chan Avatar asked Jun 05 '10 18:06

Bon_chan


3 Answers

As others have said, you need to implement IEnumerable as well as IEnumerable<T>. However, since IEnumberable<T> itself implemets IEnumerable this is trivial, just call your generic GetEnumerator():

public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;
    public IEnumerator<T> GetEnumerator()
    {
        return innerCollection.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
         return GetEnumerator();
    }
}

I'm assuming that you have methods for actually adding and removing from the innerCollection and just left them out for brevity since they didn't relate to the question at hand.

like image 116
Donnie Avatar answered Oct 20 '22 04:10

Donnie


IEnumerable and IEnumerable<T> are different interfaces and your code has to implement both. Your code only implements the GetEnumerator() of IEnumerable<T>, but not GetEnumerator() of IEnumerable. You should consider installing ReSharper which makes it easy to fix errors like this one.

like image 4
Achim Avatar answered Oct 20 '22 04:10

Achim


You need to implement a few more things:

public class ShortCollection<T> : IList<T>
{
    protected Collection<T> innerCollection;
    protected int maxSize = 10;

    #region IList<T> Members

    public int IndexOf(T item)
    {
        return innerCollection.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        innerCollection.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        innerCollection.RemoveAt(index);
    }

    public T this[int index]
    {
        get
        {
            return innerCollection[index];
        }
        set
        {
            innerCollection[index] = value;
        }
    }

    #endregion

    #region ICollection<T> Members

    public void Add(T item)
    {
        innerCollection.Add(item);
    }

    public void Clear()
    {
        innerCollection.Clear();
    }

    public bool Contains(T item)
    {
        return innerCollection.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        innerCollection.CopyTo(array, arrayIndex);
    }

    public int Count
    {
        get { return innerCollection.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(T item)
    {
        return innerCollection.Remove(item);
    }

    #endregion

    #region IEnumerable<T> Members

    public IEnumerator<T> GetEnumerator()
    {
        return innerCollection.GetEnumerator();
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return innerCollection.GetEnumerator();
    }

    #endregion
}

This code will compile... :)

like image 3
code4life Avatar answered Oct 20 '22 05:10

code4life