Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7056967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:55:48+00:00 2026-05-28T03:55:48+00:00

The List<T> class implements the IEnumerable<T> interface. It has a method GetEnumerator that returns

  • 0

The List<T> class implements the IEnumerable<T> interface. It has a method GetEnumerator that returns a List<T>.Enumerator.

I have a class as below, which gives a compile error saying the return type of GetEnumerator doesn’t match the interface.

public class InsertionSortedSet<T> : IEnumerable<T>
{
    public struct Enumerator : IEnumerator<T>
    {
        // Required interface implemented
    }

    // Other interface methods implemented

    public Enumerator GetEnumerator()
    {
        return new Enumerator(this);
    }
}

‘Entities.Helpers.InsertionSortedSet’ does not implement interface member ‘System.Collections.Generic.IEnumerable.GetEnumerator()’. ‘Entities.Helpers.InsertionSortedSet.GetEnumerator()’ cannot implement ‘System.Collections.Generic.IEnumerable.GetEnumerator()’ because it does not have the matching return type of ‘System.Collections.Generic.IEnumerator’.

Given that List<T> seems to return it’s own Enumerator class (not the interface) and yet it does implement the Enumeration<T> interface I’m confused, as I can’t see what i’ve got different to that class.

What’s wrong with my setup that makes it fail, where List<T> works?


I want to return a InsertionSortedSet<T>.Enumerator rather than the interface as it avoids boxing, which I need to cut out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:55:49+00:00Added an answer on May 28, 2026 at 3:55 am

    It’s complaining because GetEnumerator() returns IEnumerator<T> for the IEnumerable<T> interface. To satisfy, your type must return IEnumerator<T> (and an explicit one for IEnumerator as well).

    But, many times it’s desirable for a class to return a more specific type than the interface specifies, but interfaces don’t allow for covariant return types like that. So to do this, you can do what List<T> does and have GetEnumerator() return your specific enumerator, but you must also implement explicit implementations for IEnumerable.GetEnumerator() that returns an IEnumerator and for IEnumerable<T>.GetEnumerator() that returns an IEnumerator<T>:

        // This is your specific version, like List<T> does
        public Enumerator GetEnumerator()
        {
            return new Enumerator(this);
        }
    
        // This is the one with the return value IEnumerator<T> expects
        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return new Enumerator(this);
        }
    
        // Plus, it also expects this as well to satisfy IEnumerable
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    

    If you look at List<T> you’ll see that it implements GetEnumerator() three times:

    • Once explicitly for IEnumerable<T>
    • Once explicitly for Enumerable
    • Once with a very list-specific return

    You could do the same in your class if you wish (which it sould like is the way you began) but if you do that you must explicitly implement IEnumerable<T>.GetEnumerator() and IEnumerable.GetEnumerator()

    If you navigate to the definition, you can see the other definitions in the object browser by selecting the different interfaces it satisfies (or by assigning a List<T> instance to an IEnumerable or IEnumerable<T> reference and going to definition):

    Screenshot of Object Explorer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically i have a container which implements IEquatable (sample shown below) public class ContainerClass
I'm creating a specialised proxy class that implements IList<T> and wraps an internal List<T>
I have a list of class items List<MyClass> I have a seperate object that
I have a class, which is just a wrapper over a list, i.e., public
I have a project that is using a generic list of a custom class.
I'm currently trying to create a class which implements IEnumerable<T> in order to construct
I have a static class 'Defaults' which shall hold default matrices that are forwarded
I have a generic class that has one type parameter (T). I needed to
Please find below my code. Employee class implements IEmployee interface. namespace MiddleWare.ServiceContracts { [ServiceContract(Namespace
I've written a CustomerCollection class, which implements the IEnumerable and IEnumerator interfaces. Now I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.