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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:23:49+00:00 2026-05-11T03:23:49+00:00

When foreach ing through a generic list I often want to do something different

  • 0

When foreaching through a generic list I often want to do something different for the first element in the list:

List<object> objs = new List<object> {     new Object(),     new Object(),     new Object(),     new Object() };  foreach (object o in objs) {     if (o == objs.First())     {         System.Diagnostics.Debug.WriteLine('First object - do something special');     }     else     {         System.Diagnostics.Debug.WriteLine('object Do something else');     } } 

This will output:

     First object - do something special     object Do something else     object Do something else     object Do something else 

This is all fine and dandy.

However if my generic list is of a value type, this approach will fail.

List<int> ints = new List<int> { 0, 0, 0, 0 }; foreach (int i in ints) {     if (i == ints.First())     {         System.Diagnostics.Debug.WriteLine('First int - do something special');     }     else     {         System.Diagnostics.Debug.WriteLine('int Do something else');     } } 

This will output:

     First int - do something special     First int - do something special     First int - do something special     First int - do something special 

Now I know I could recode this to add a boolean flag variable or traditional for loop, but I am wondering if there’s any way to find out if a foreach loop is on the first iteration of its looping.

  • 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-11T03:23:50+00:00Added an answer on May 11, 2026 at 3:23 am

    Well, you could code it using explicit iteration:

    using(var iter = ints.GetEnumerator()) {   if(iter.MoveNext()) {      // do 'first' with iter.Current       while(iter.MoveNext()) {        // do something with the rest of the data with iter.Current      }   } } 

    The bool flag option (with foreach) is probably easier though… that is what I (almost) always do!

    Another option would be LINQ:

    if(ints.Any()) {   var first = ints.First();   // do something with first }  foreach(var item in ints.Skip(1)) {   // do something with the rest of them } 

    The downside of the above is that it tries to look at the list 3 times… since we know it is a list, that is fine – but if all we had was an IEnumerable<T>, it would only be sensible to iterate it once (since the source might not be re-readable).

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

Sidebar

Related Questions

Is there a way to iterate (through foreach preferably) over a collection using reflection?
foreach ($_GET as $field => $label) { $datarray[]=$_GET[$field]; echo $_GET[$field]; echo <br>; } print_r($datarray);
foreach($arrayOne as $value){ do function } In the above example, I'd like to pass
I have a 'foreach' macro I use frequently in C++ that works for most
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
Is there a foreach structure in MATLAB? If so, what happens if the underlying
Within a Foreach loop in SSIS is there a way that if a task
Which browsers other than Firefox support Array.forEach()? Mozilla say it's an extension to the
Is it possible to find the foreach index? in a for loop as follows:

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.