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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:10:17+00:00 2026-05-13T18:10:17+00:00

I often find myself doing the following index-counter messiness in a foreach loop to

  • 0

I often find myself doing the following index-counter messiness in a foreach loop to find out if I am on the first element or not. Is there a more elegant way to do this in C#, something along the lines of if(this.foreach.Pass == 1) etc.?

int index = 0;
foreach (var websitePage in websitePages) {
    if(index == 0)
        classAttributePart = " class=\"first\"";
    sb.AppendLine(String.Format("<li" + classAttributePart + ">" + 
        "<a href=\"{0}\">{1}</a></li>", 
        websitePage.GetFileName(), websitePage.Title));
    index++;
}
  • 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-13T18:10:17+00:00Added an answer on May 13, 2026 at 6:10 pm

    Another approach is to accept that the “ugly part” has to be implemented somewhere and provide an abstraction that hides the “ugly part” so that you don’t have to repeat it in multiple places and can focus on the specific algorithm. This can be done using C# lambda expressions (or using C# 2.0 anonymous delegates if you’re restricted to .NET 2.0):

    void ForEachWithFirst<T>(IEnumerable<T> en, 
         Action<T> firstRun, Action<T> nextRun) {
      bool first = true;
      foreach(var e in en) {
        if (first) { first = false; firstRun(e); } else nextRun(e);
      }
    }
    

    Now you can use this reusable method to implement your algorithm like this:

    ForEachWithFirst(websitePages,
      (wp => sb.AppendLine(String.Format("<li class=\"first\">" +
             "<a href=\"{0}\">{1}</a></li>", wp.GetFileName(), wp.Title)))
      (wp => sb.AppendLine(String.Format("<li>" + 
             "<a href=\"{0}\">{1}</a></li>", wp.GetFileName(), wp.Title))) );
    

    You could design the abstraction differently depending on the exact repeating pattern. The good thing is that – thanks to lambda expression – the structure of the abstraction is completely up to you.

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

Sidebar

Related Questions

I find myself doing the following often enough that I feel like there must
Often I find myself doing the following: print Input text: input = gets.strip Is
So I find myself doing something like the following pattern often. Instead of: if
In my Python code I often find myself doing the following (using DB-API): yValues
I often find myself doing something along the following lines in sql server 2005
Ok, I find myself doing this often. I'll attach ids to my tables and
I often find myself doing things like this when manipulating tables:- $($('table tr').children()[2]).html(); For
I often find myself doing something like this a lot: something | grep cat
Sometimes I find myself doing the following: if (some_condition) set_flag(true) do_some_work(); // more work
I find myself doing Nyy very often to yank the current line and N-1

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.