The generic list class has a .ForEach(Action<T> action)
method. Now i've done some simple timings of how they both perform and it seems that the generic ForEach is the poorer performer. The (Snippet Compiler Friendly) code is 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 comes out at 177ms and GFOREACH at 707ms.
Now I'm guessing there's a good reason for using it but i just can't think of one. Clearly performance isn't the reason so the question is when would it be the best option?
Thanks in advance.