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 5955109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:04:01+00:00 2026-05-22T18:04:01+00:00

I am developing a collection class, which should implement IEnumerator and IEnumerable. In my

  • 0

I am developing a collection class, which should implement IEnumerator and IEnumerable.

In my first approach, I implemented them directly. Now I have discovered the yield keyword, and I have been able to simplify everything a whole lot substituting the IEnumerator/IEnumerable interfaces with a readonly property Values that uses yield to return an IEnumerable in a loop.

My question: is it possible to use yield in such a way that I could iterate over the class itself, without implementing IEnumerable/IEnumerator?

I.e., I want to have a functionality similar to the framework collections:

List<int> myList = new List<int>();
foreach (int i in myList)
{
    ...
}

Is this possible at all?

Update: It seems that my question was badly worded. I don’t mind implementing IEnumerator or IEnumerable; I just thought the only way to do it was with the old Current/MoveNext/Reset methods.

  • 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-22T18:04:02+00:00Added an answer on May 22, 2026 at 6:04 pm

    You won’t have to implement IEnumerable<T> or IEnumerable to get foreach to work – but it would be a good idea to do so. It’s very easy to do:

    public class Foo : IEnumerable<Bar>
    {
        public IEnumerator<Bar> GetEnumerator()
        {
            // Use yield return here, or 
            // just return Values.GetEnumerator()
        }
    
        // Explicit interface implementation for non-generic
        // interface; delegates to generic implementation.
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
    

    The alternative which doesn’t implement IEnumerable<T> would just call your Values property, but still providing a GetEnumerator() method:

    public class Foo
    {
        public IEnumerator<Bar> GetEnumerator()
        {
            // Use yield return here, or 
            // just return Values.GetEnumerator()
        }
    ]
    

    While this will work, it means you won’t be able to pass your collection to anything expecting an IEnumerable<T>, such as LINQ to Objects.

    It’s a little-known fact that foreach will work with any type supporting a GetEnumerator() method which returns a type with appropriate MoveNext() and Current members. This was really to allow strongly-typed collections before generics, where iterating over the collection wouldn’t box value types etc. There’s really no call for it now, IMO.

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

Sidebar

Related Questions

When developing whether its Web or Desktop at which point should a developer switch
When developing a new web based application which version of html should you aim
Suppose we are developing class which implements simple CRUD operations for working with DB.
I have a class that I need to Unit Test. For background I'm developing
UPDATED (12/17/2009): Now reflects latest progress I've made. This is the first application that
In the Design Guidelines for Developing Class Libraries , Microsoft say: Do not assign
I'm developing my first ASP.NET MVC app and what I'd like to accomplish is
I have a simple program I'm developing to perform some bandwidth tests on remote
Is it necessary or advantageous to write custom connection pooling code when developing applications
Developing websites are time-consuming. To improve productivity, I would code a prototype to show

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.