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

  • Home
  • SEARCH
  • 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 109977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:09:16+00:00 2026-05-11T02:09:16+00:00

I was wondering if there are any times where it’s advantageous to use an

  • 0

I was wondering if there are any times where it’s advantageous to use an IEnumerator over a foreach loop for iterating through a collection? For example, is there any time where it would be better to use either of the following code samples over the other?

IEnumerator<MyClass> classesEnum = myClasses.GetEnumerator(); while(classesEnum.MoveNext())     Console.WriteLine(classesEnum.Current); 

instead of

foreach (var class in myClasses)     Console.WriteLine(class); 
  • 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. 2026-05-11T02:09:16+00:00Added an answer on May 11, 2026 at 2:09 am

    First, note that one big difference in your example (between foreach and GetEnumerator) is that foreach guarantees to call Dispose() on the iterator if the iterator is IDisposable. This is important for many iterators (which might be consuming an external data feed, for example).

    Actually, there are cases where foreach isn’t as helpful as we’d like.

    First, there is the ‘first item’ case discussed here (foreach / detecting first iteration).

    But more; if you try writing the missing Zip method for stitching two enumerable sequences together (or the SequenceEqual method), you find that you can’ use foreach on both sequences, since that would perform a cross-join. You need to use the iterator directly for one of them:

    static IEnumerable<T> Zip<T>(this IEnumerable<T> left,     IEnumerable<T> right) {     using (var iter = right.GetEnumerator())     {         // consume everything in the first sequence         foreach (var item in left)         {             yield return item;              // and add an item from the second sequnce each time (if we can)             if (iter.MoveNext())             {                 yield return iter.Current;             }         }         // any remaining items in the second sequence         while (iter.MoveNext())         {             yield return iter.Current;         }                     }             }  static bool SequenceEqual<T>(this IEnumerable<T> left,     IEnumerable<T> right) {     var comparer = EqualityComparer<T>.Default;      using (var iter = right.GetEnumerator())     {         foreach (var item in left)         {             if (!iter.MoveNext()) return false; // first is longer             if (!comparer.Equals(item, iter.Current))                 return false; // item different         }         if (iter.MoveNext()) return false; // second is longer     }     return true; // same length, all equal             } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 59k
  • Answers 59k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer From the KB: This behavior occurs because a security fix… May 11, 2026 at 9:00 am
  • added an answer I know this is only a workaround, but you can… May 11, 2026 at 9:00 am
  • added an answer Scott Gu had an article on this once. The solution… May 11, 2026 at 9:00 am

Related Questions

I was wondering if there are any alternatives to Microsoft's SQL Server Management Studio?
I was wondering if there are any times where it's advantageous to use an
So I was wondering if there are any major differences between the various implementations
while writing a custom attribute in C# i was wondering if there are any
I was wondering if InputManagers are still part of Leopard and if there is
I was wondering if there is a better way to cope with MS-Access' inability
I was wondering if there was an easy way in SQL to convert an
I was wondering if there was somewhere I could get some starter kit /
I was wondering if there was a .NET-compatible CLR that was implemented using the
I was wondering if there was a way to get at the raw HTTP

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.