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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:28:21+00:00 2026-05-16T17:28:21+00:00

I have a function running over an enumerable, but the function should be a

  • 0

I have a function running over an enumerable, but the function should be a little bit different for the first item, for example:

void start() { 
    List<string> a = ...
    a.ForEach(DoWork);
}

bool isFirst = true;

private void DoWork(string s) {
   // do something

   if(isFirst)
     isFirst = false;
   else
     print("first stuff");

   // do something
}

How would you refactor this to avoid that ugly flag?

  • 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-16T17:28:21+00:00Added an answer on May 16, 2026 at 5:28 pm

    EDIT: added usage example, added a ForFirst method, reordered my paragraphs.

    Below is a complete solution.

    Usage is either of the following:

            list.ForFirst(DoWorkForFirst).ForRemainder(DoWork);
            // or 
            list.ForNext(1, DoWorkForFirst).ForRemainder(DoWork);
    

    The crux is the ForNext method, which performs an action for the specified next set of items from the collection and returns the remaining items. I’ve also implemented a ForFirst method that simply calls ForNext with count: 1.

    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>();
            // ...
    
            list.ForNext(1, DoWorkForFirst).ForRemainder(DoWork);
        }
    
        static void DoWorkForFirst(string s)
        {
            // do work for first item
        }
    
        static void DoWork(string s)
        {
            // do work for remaining items
        }
    }
    
    public static class EnumerableExtensions
    {
        public static IEnumerable<T> ForFirst<T>(this IEnumerable<T> enumerable, Action<T> action)
        {
            return enumerable.ForNext(1, action);
        }
    
        public static IEnumerable<T> ForNext<T>(this IEnumerable<T> enumerable, int count, Action<T> action)
        {
            if (enumerable == null)
                throw new ArgumentNullException("enumerable");
    
            using (var enumerator = enumerable.GetEnumerator())
            {
                // perform the action for the first <count> items of the collection
                while (count > 0)
                {
                    if (!enumerator.MoveNext())
                        throw new ArgumentOutOfRangeException(string.Format(System.Globalization.CultureInfo.InvariantCulture, "Unexpected end of collection reached.  Expected {0} more items in the collection.", count));
    
                    action(enumerator.Current);
    
                    count--;
                }
    
                // return the remainder of the collection via an iterator
                while (enumerator.MoveNext())
                {
                    yield return enumerator.Current;
                }
            }
        }
    
        public static void ForRemainder<T>(this IEnumerable<T> enumerable, Action<T> action)
        {
            if (enumerable == null)
                throw new ArgumentNullException("enumerable");
    
            foreach (var item in enumerable)
            {
                action(item);
            }
        }
    }
    

    I felt a bit ridiculous making the ForRemainder method; I could swear that I was re-implementing a built-in function with that, but it wasn’t coming to mind and I couldn’t find an equivalent after glancing around a bit. UPDATE: After reading the other answers, I see there apparently isn’t an equivalent built into Linq. I don’t feel so bad now.

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

Sidebar

Related Questions

I have this code running in my onDestroy function: @Override protected void onDestroy() {
I want to have a java app running, using a function/method (with as little
I have a function @Override public void run() { while(running && (!eof)){ if(surfaceHolder.getSurface().isValid()){ Canvas
i have small function that running on condintion, if text box value is not
I have a PL/SQL function (running on Oracle 10g) in which I update some
I have the following function sending an email twice (and I believe running if($result)
I have a program in Octave that has a loop - running a function
Suppose you have the following function foo . When I'm running a for loop,
Code is not running on .click when I have this: $(.cancel).click(function() { alert(got here);
I have this code running great in ff, opera and chrome: <script type=text/javascript> $(document).ready(function(){

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.