Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to implement IEnumerable<T> interface?

I took this code from a textbook and it does not compile and I’m completely stuck.

I feel like if you’re an experienced C# user you will know the solution right away. Could you please take a quick look at it. Here’s a link to playground

Error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Code

using System.Collections.Generic;
                    
#nullable enable

public record LinkedListNode<T>(T Value)
{   
    public LinkedListNode<T>? Next { get; internal set; }
    public LinkedListNode<T>? Prev { get; internal set; }
    public override string? ToString() => Value?.ToString();
}


public class LinkedList<T> : IEnumerable<T>
{
  public LinkedListNode<T>? First { get; private set; }
  public LinkedListNode<T>? Last { get; private set; }
  public LinkedListNode<T> AddLast(T node)
  {
    LinkedListNode<T> newNode = new(node);
    if (First is null || Last is null)
    {
      First = newNode;
      Last = First;
    }
    else
    {
      newNode.Prev = Last;
      Last.Next = newNode;
      Last = newNode;
    }
    return newNode;
  }
  public IEnumerator<T> GetEnumerator()
  {
    LinkedListNode<T>? current = First;
    while (current is not null)
    {
      yield return current.Value;
      current = current.Next;
    }
  }
}

>Solution :

IEnumerable<T> derives from IEnumerable (non generic). So if you want to implement the first, you also have to implement the members of the second.

IEnumerable.GetEnumerator() must thus also be implemented – as the compiler hints.

However, this will not work:

public class LinkedList<T> : IEnumerable<T>
{
    // ... existing code

    public System.Collections.IEnumerator GetEnumerator() { /* ... */ }
}

Because from the compiler’s point of view method by that name (and parameters) already exists. Overload resolution does not consider the return type of the method.

The solution is to implement the "ambigous" method using explicitl interface implementation

public class LinkedList<T> : IEnumerable<T>
{
    // ... existing code

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading