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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:40:20+00:00 2026-05-22T20:40:20+00:00

Hi I have had a problem with some method that should perform action on

  • 0

Hi I have had a problem with some method that should perform action on some IEnumerator object instance, the code sample shows the case:

class Program
    {        
        static void Main(string[] args)
        {
            string[] testArray = { "Item1", "Item2" };

            var query = testArray.Select((item, index) => new { index, item });

            Console.WriteLine("Query run 1, {0}", Environment.NewLine);

            foreach (var obj in query)
            {
                Console.WriteLine("{0}", obj);
            }

            Console.WriteLine("Query run 2, {0}", Environment.NewLine);

            doAction<object>(query as IEnumerator<object>, obj => Console.WriteLine(obj) );


            Console.WriteLine("Query run 3 ( same as run 1), {0}", Environment.NewLine);

            foreach (var obj in query)
            {
                Console.WriteLine("{0}", obj);
            }

            Object tmp = Console.ReadLine();
        }


        private static void doAction<T>(IEnumerator<T> enumerator, Action<T> action)
        {

            if (enumerator == null)
            {
                throw new ArgumentNullException("enumerator");
            }

            if (action == null)
            {
                throw new ArgumentNullException("action");
            }


            while (enumerator.MoveNext())
            {
                action(enumerator.Current);
            }

        }
    }

The output:

Query run 1,

{ index = 0, item = Item1 }
{ index = 1, item = Item2 }
Query run2,

Query run 3 ( same as run 1), 

{ index = 0, item = Item1 }
{ index = 1, item = Item2 }

Does anyone know what method doAction do not take action ?

The real problem is that i need the answer in English language, because my is really bad and i can not explain why this is happening to my mate.

Thanks in advance.

Thnaks.

  • 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-22T20:40:21+00:00Added an answer on May 22, 2026 at 8:40 pm

    Yes – you shouldn’t be using IEnumerator<T> directly, only IEnumerable<T>. Basically you’re seeing an oddity of the way that the C# compiler implements iterator blocks: there’s one type which implements both IEnumerable<T> and IEnumerator<T>. That’s unusual in itself, but is done for performance reasons. It’s not something you should take advantage of or rely on.

    Fundamentally, casting an IEnumerable<T> to IEnumerator<T> is a bad idea – you shouldn’t do it. They’re different interfaces representing subtly different ideas – the difference between “a sequence which can be iterated over” and “something which is iterating over a sequence”.

    If you really want to use IEnumerator<T> as the parameter type for doAction (which would normally be DoAction, btw) just call GetEnumerator() instead:

    using (var iterator = query.GetEnumerator())
    {
        doAction<object>(iterator, obj => Console.WriteLine(obj) );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little problem. I had some JComboBox to a JDialog but they
(I have a problem that I illustrated in this question but had no correct
I have a tricky problem that has had me scratching my head for a
Ok I have this problem that I've never had before, it's really bugging me.
I have had this problem w/ two seperate WYSWYG editors in my rails application
I have had the following problem for a while and I am really not
For years, on 32-bit systems I have never had a problem. Why can't I
I have run into a bit of a problem here: I had a problem-specific
The responses I've got to this question have solved the problem I had in
can anyone help, i have problem doing a sort, I thought i had it

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.