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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:11:04+00:00 2026-05-26T00:11:04+00:00

I have a code like: class T : IEnumerable, IEnumerator { private int position

  • 0

I have a code like:

        class T : IEnumerable, IEnumerator
        {
            private int position = -1;

            public T() { }

            public IEnumerator GetEnumerator() { return this; }

            public object Current { get { return position; } }

            public bool MoveNext()
            {
                position++;
                return (position < 5);
            }

            public void Reset() { position = -1; }
        }

//Using in code:
T t = new T();
foreach (int i in t)
 //to do something

In the code above all is working fine but when I use next:

foreach (int i in t)
   if (i == 2)
     foreach (int p in t)
       //print p
   else
       //print i

It prints (in brackets second loop): 0 1 (3 4) 2 instead of 0 1 (0 1 2 3 4) 2 3 4
I tested It on List and Collection and they do It right.
How can I to achive what I need?

  • 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-26T00:11:05+00:00Added an answer on May 26, 2026 at 12:11 am

    You can’t because you have made your code surface a single enumerator, itself a mistake IMO. A better version would be, for me:

    class T : IEnumerable<int> {
        public IEnumerator<int> GetEnumerator() {
            int i = 0;
            while(i < 5) {
                yield return i;
                i++;
            }
        }
        IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
    }
    

    The compiler will create the right devices to achieve this with separate enumerators.

    Unless you are writing for .NET 1.1, then if you find yourself manually writing an enumarator, there’s a very good chance that you are doing it the hard way, and getting it wrong as a bonus.

    If you really must do it the hard way:

    class T : IEnumerable<int>
    {
        public T() { }
    
        public IEnumerator<int> GetEnumerator() { return new TEnumerator(); }
        IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
        private class TEnumerator : IEnumerator<int>
        {
            private int position = -1;
            public int Current { get { return position; } }
            object IEnumerator.Current { get { return Current; } }
            void IDisposable.Dispose() {}
            public bool MoveNext()
            {
                position++;
                return (position < 5);
            }
            public void Reset() { position = -1; }
        } 
    }
    

    The significance here is that different instances of TEnumerator allow the same T instance to be iterated separately.

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

Sidebar

Related Questions

I have some code like this: public class EffectValues : IEnumerable<object> { public object
How can I prevent from concurrent access. I have code like this public class
I have code that looks something like this: public class Foo<T> : ObservableCollection<T> {
Suppose I have code like this: template<class T, T initial_t> class Bar { //
Say I have some code like namespace Portal { public class Author { public
I have a data structure that represents C# code like this: class Namespace: string
If I have code like this: public XALServiceConfiguration CreateInstance() { var config = ConfigurationManager.GetSection(ConfigurationSectionName)
I have code like: @Entity @Table(name = A) @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) public class A
I have a Class like this: class ClassA { public long classAID {get; set;}
I have a ton of repeating code in my class that looks like the

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.