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

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • 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
  • Editorial Team
    Editorial Team added an answer You might be better off redesigning the table so that… May 11, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer There are two ways to do programmatic animation within the… May 11, 2026 at 6:50 pm
  • Editorial Team
    Editorial Team added an answer You have multiple options to group your methods: By functionality… May 11, 2026 at 6:50 pm

Related Questions

I believe there is a way to do this, but I'm not familiar with
I'm using Linq to SQL. I have a DataContext against which I am .SubmitChanges()'ing.
When you use Visual Studio's code analysic (FxCop), and want to suppress a message
The project I'm working on has two type of accounts, people and companies .

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.