0
class CustomList<T> : IList<T> {
    List<T> StorageList;

    public int Count{ get => StorageList.Count; }
    
    public bool IsReadOnly { get => false; }
    
    public CustomList() {
        StorageList = new List<T>();
    }

    public CustomList(IEnumerable<T> collection) {
        StorageList = new List<T>(collection);
    }

    public CustomList(int capacity) {
        StorageList = new List<T>(capacity);
    }

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

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

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

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

    public List<T>.Enumerator GetEnumerator() {
        return StorageList.GetEnumerator();
    }

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

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

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

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

    public T this[int index] { 
        get => StorageList[index];
        set => StorageList[index] = value;
    }
}

Here my class which implemetns IList<T> interface from System.Collections.Generic.

This codes causes CS0738 and according to CS0738, I didn't implement IEnumerable<T>.GetEnumerator().

'CustomList<T>' does not implement interface member 'IEnumerable<T>.GetEnumerator()'. 'CustomList<T>.GetEnumerator()' cannot implement 'IEnumerable<T>.GetEnumerator()' because it does not have the matching return type of ' IEnumerator<T>'.

I can't redefine GetEnumerator() because I defined it to implement IList<T>.GetEnumerator(). How should I implement IEnumerable<T>.GetEnumerator()?

  • google for explicit interface implementation – Michael Welch Jul 29 '22 at 18:53
  • You defined `GetEnumerator` to return a `List.Enumerator` rather than an `IEnumerator`. You may have confused this with an explicit interface implementation, but that would be `IList.GetEnumerator()`, not the return type. – Jeroen Mostert Jul 29 '22 at 18:53
  • For example: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation – Michael Welch Jul 29 '22 at 18:53
  • 1
    You will need to implement both the generic and the non-generic version, and I'd recommend you implement both only explicitly, so `IEnumerator IEnumerable.GetEnumerator() => StorageList.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => StorageList.GetEnumerator();`. – Jeroen Mostert Jul 29 '22 at 18:57
  • Does this answer your question? [Inheritance from multiple interfaces with the same method name](https://stackoverflow.com/questions/2371178/inheritance-from-multiple-interfaces-with-the-same-method-name) – Thomas Weller Jul 29 '22 at 18:58
  • Hey @JeroenMostert ! I fallowed your advice and it worked. Thanks a lot. – Ufuk Can İşbildi Jul 29 '22 at 20:02

0 Answers0