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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:15:38+00:00 2026-05-18T08:15:38+00:00

Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> I’m wondering whether there is a

  • 0

Possible Duplicate:
LINQ equivalent of foreach for IEnumerable<T>

I’m wondering whether there is a method for IEnumerable like the following .Each() in the .Net library

var intArray = new [] {1, 2, 3, 4};
intArrary.Each(Console.WriteLine);

I know I can use a foreach loop or easily write an extension method like this:

public static class EnumerableExtensions
{
    public static void Each<T>(this IEnumerable<T> enumberable, Action<T> action)
    {
        foreach (var item in enumberable)
        {
            action(item);
        }
    }
}

But I’m hoping not to create my own method to mess up code if there is already such an extension method in the library. And something like .Each() (with a few overloadings which can take conditions as extra params) is heavily needed by programmers, and there should already be one. Am I correct?

Update

Ruby developers may recognize it as a .each() iterator. And that’s what I hope to have in C#. Maybe C# can have more iterator methods like those in Ruby.

  • 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-18T08:15:38+00:00Added an answer on May 18, 2026 at 8:15 am

    As others have said there is none built in on IEnumerable<T>. The Linq team was against it as per this post by Eric Lippert::
    http://blogs.msdn.com/b/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx

    There is a static method on Array.ForEach and List<T> has an instance method. There is also in PLINQ foreach like statements, but be warned that they work in parallel and can lead to very bad performance for extremely simple actions.
    Here is one such method in PLINQ: http://msdn.microsoft.com/en-us/library/dd383744.aspx

    And here is a guide on PLINQ in general: http://msdn.microsoft.com/en-us/library/dd460688.aspx

    While I can’t find the exact article if you poke around in the ParrallelEnumerable section it gives warnings and tips as to how to improve the performance of using parallelism in code

    If you want it, I suggest creating 2 versions, one that include indexer and one without. This can be quite useful and can save a select statement to acquire the index.
    e.g.

    public static void ForEach<T>(IEnumerable<T> enumerable,Action<T> action)
    {
        foreach(var item in enumerable) action(item);
    }
    
    public static void ForEach<T>(IEnumerable<T> enumerable,Action<T,int> action)
    {
        int index = 0;
        foreach(var item in enumerable) action(item,index++);
    }
    

    I’d also include argument validation as these are public methods.

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

Sidebar

Related Questions

Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> please help me to figure out
Possible Duplicate: .NET LINQ query syntax vs method chain Are there any performance differences
Possible Duplicate: Find sequence in IEnumerable<T> using Linq There is a string method: int
Possible Duplicate: Equivalent of SQL ISNULL in LINQ? I am recently migrated from ADO.Net
Possible Duplicate: What is the Java equivalent for LINQ? There are numerous questions asking
Possible Duplicate: Linq - Top value form each group I have data in the
Possible Duplicate: Generating all Possible Combinations Is there a good LINQ way to do
Possible Duplicate: Linq: “Or” equivalent of Where() I posted a question about a week
Possible Duplicate: How to update Linq to SQL dbml file? Is there a quick
Possible Duplicate: Is there a more efficent way to randomise a set of LINQ

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.