.Net - When is List <T> .ForEach preferable to a standard foreach loop?
The general list class has a .ForEach(Action<T> action) method. Now I have made some simple timings of how they work, and it seems that the common ForEach is a poorer performer. Code (Snippet Compiler Friendly) below -
public static class timer{ public static long foreachloop = 0; public static long Gforeachloop = 0;} public class something{ public List<string> myStrings = new List<string>(); public something() { for(int i = 1; i<=5000000;i++) { myStrings.Add(i.ToString()); } }} public class cls1{ private static List<string> Strings = new List<string>(); private static List<string> OtherStrings = new List<string>(); public static void RunSnippet() { something s = new something(); Stopwatch watch = new Stopwatch(); watch.Start(); foreach(string x in s.myStrings) { Strings.Add(x); } watch.Stop(); timer.foreachloop = watch.ElapsedMilliseconds; watch.Reset(); watch.Start(); s.myStrings.ForEach(delegate(string n){OtherStrings.Add(n);}); s.myStrings.Clear(); watch.Stop(); timer.Gforeachloop = watch.ElapsedMilliseconds; WL("FOREACH-"+timer.foreachloop + ",Count = " + Strings.Count); WL("GFOREACH-"+timer.Gforeachloop + ",Count = " + OtherStrings.Count); } #region Helper methods public static void Main() { try { RunSnippet(); } catch (Exception e) { string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString()); Console.WriteLine(error); } finally { Console.Write("Press any key to continue..."); Console.ReadKey(); } } private static void WL(object text, params object[] args) { Console.WriteLine(text.ToString(), args); } private static void RL() { Console.ReadLine(); } private static void Break() { System.Diagnostics.Debugger.Break(); } #endregion } FOREACH goes 177 ms and GFOREACH 707 ms.
Now I guess there is a good reason to use it, but I just can't think about it. Obviously, performance is not the reason, so the question is when will this be the best option?
Thanks in advance.
This blog post from Eric Lippert gives the backdrop:
http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx
He talks about the general assumption of an extension method to do the same for IEnumerable<T> , but a philosophical objection applies to List<T>.ForEach .
This suggests that perhaps this method has never been such a good idea, although it looks "cool." It is much easier to use foreach .
I suggested that such methods can be described as a correction for the classic closing error per cycle. ./p>
But in practice, I only better understood such errors.
When he looks neat.
Not a joke at all. In fact, I mean. Go with a more readable style in your case. For example, if you just want to call a method for each element, for example:
list.ForEach(Console.WriteLine); This style fits better. However, if you have one hundred lines as the body of the loop, or if you have nested loops and the construction of thread flows, the old style looks better.