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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:56:57+00:00 2026-06-10T00:56:57+00:00

I have a foreach loop that needs converting to a for or while loop.

  • 0

I have a foreach loop that needs converting to a for or while loop. My loop looks like this:

foreach (Item item in Items)
{
    // some stuff
}

What is the equivalent for or while loop?

I think I need to use GetEnumerator to get an IEnumerator<Item>, but I don’t know how to use it properly. Items isn’t a list, otherwise I’d use indexing.

  • 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-06-10T00:56:57+00:00Added an answer on June 10, 2026 at 12:56 am

    In the simplest case(no disposing etc.) you can use:

    var enumerator = Items.GetEnumerator();// `var` to take advantage of more specific return type
    while(enumerator.MoveNext())
    {
      Item item = enumerator.Current;
      ...
    }
    

    For the exact rules check the C# specification 8.8.4 The foreach statement.

    A foreach statement of the form

    foreach (V v in x) embedded-statement
    

    is then expanded to:

    {
      E e = ((C)(x)).GetEnumerator();
      try {
         V v;
         while (e.MoveNext()) {
            v = (V)(T)e.Current;
                    embedded-statement
        }
      }
      finally {
         … // Dispose e
      }
    

    }

    (Quoted from the C# Language Specification Version 4.0)

    The types using here are: “a collection type C, enumerator type E and element type T“. E is the return type of GetEnumerator, and not necessarily IEnumerator<V> since foreach uses duck typing. The spec also describes a number of corner cases and how to infer these types, but those details are probably not relevant here.

    In C# 5 the declaration of v will be moved into the while loop to get more intuitive closure semantics.

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

Sidebar

Related Questions

I have a foreach loop that looks like this: foreach (var line in theCP4UnknownList.Distinct())
I have a loop that can look like this: For Each article In artAll
Right now I have a foreach loop that grabs the first link with an
in a ASP.NET application (MVC) I have a foreach loop that loops through a
I have a simple Parallel.Foreach loop that has about 1000 rows in the DataTable,
In PHP I can use a foreach loop such that I have access to
I have a loop that enters each non-empty text field into a database: foreach
I have a datatable that I add to the chart in foreach loop as
i have two foreach loop to display some data, but i want to use
I have a For Each loop that is looping through an array of strings

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.