开发者

Foreach/For loop alternative lambda function?

We use for or foreach to loop through collections and process each entries.

Is there any alternative in all those new lambda functions for collections in C#?

Traditional way of doing

foreach(var v in vs)
{
  Console.write(v);
}
开发者_开发问答

Is there anything like?

vs.foreach(v => console.write(v))


List has the ForEach method, however, IEnumerable does not.

There are a number of questions / answers regarding this. I think the main reason it was not implemented in IEnumerable though is that Linq on Enumerables is "meant" to be side effect free as it's a querying language.

Eric Lippert explains his thoughts on his blog.

http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx


Yes.

Of of IList, there is a ForEach extension method:

There is a ForEach method for List:

List<string> list = new List<string>() { "1", "2", "3", "4" };

list.ForEach(s => Console.WriteLine(s));

Is that what you're looking for?


List(T).ForEach does exactly that.


Eric makes a compelling argument except for the fact that there is a ForEach for List<T> but only for List<T>. I've fouund numerous occassions to use that very useful extension method and been equally frustrated by the lack of a general extension method for IEnumerable<T>, so I finally decided to write my own.

In doing so, I ended up going one step further and writing a Once<T> and ForEvery<T> fluent extension methods that allow chaing of actions on each item and the execution of an action just once in that chain, if for no other reason than they seemed like a good idea at the time.

Here's the code:

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

   public static IEnumerable<T> Once<T>(this IEnumerable<T> collection, 
      Func<IEnumerable<T>, bool> predicate, Action<IEnumerable<T>> action)
   {
      if (predicate(collection)) 
         action(collection);
      return collection;
   }

   public static IEnumerable<T> ForEvery<T>(this IEnumerable<T> collection,
      Func<T, bool> predicate, Action<T> action)
   {
      foreach (var item in collection)
      {
         if (predicate(item)) 
            action(item);
      }
      return collection;
   }
}

You can find example usage code on my blog post Extend IEnumerable with ForEach and Once Methods.


By default, there isn't one.
You could define your own:

public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) {  
   foreach(T val in source)  
      action(val);  
}


What you've written is perfectly legal (apart from the case) - assuming that the type of vs has ForEach defined as an extension method or if it's a List.

ForEach is defined for List<T>:

Performs the specified action on each element of the List<T>.


vs.ToList().ForEach(item => Console.WriteLine(item));


.NET 3.5's Linq doesn't have Foreach, though LinqBridge provided one. I got accustomed to using Linqbridge that I naturally assumed it is part of .NET 3.5 Framework

Just add it to your extension methods collections if you think it could bring clarity to your program

static class Helper
{
    public static IEnumerable<t> ForEeach<t>
        (this IEnumerable<t> source, Action<t> act)
    {
        foreach (T element in source) act(element);
        return source;
    }
}


static void Main(string[] args)  
{
    int[] i = {1, 2, 3, 4, 5, 6, 7, 78, 8, 9};
    var data = i.ToList();

    data.ForEach(m => { Console.WriteLine(m); });

    Console.ReadKey();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜