Lately, I’ve been working a lot in .NET 2.0; specially with generic collections.  After working with System.Collections.Generics.List, I’ve learned to appreciate its ForEach method.  Oh, how much it reminds me of the map procedure in Scheme.  Can you tell which code is faster?

namespace GenericList
{
    
using System;
    using 
System.Collections.Generic;
    using 
System.Diagnostics;

    class 
Program
    {
        
static void Main(string[] args)
        {
            
string[] numbers "1""2""3""4""5"};
            
List<string> numberList = new List<string>(numbers);

            
// Create a watch to time the execution!
            
Stopwatch watch = new Stopwatch();
            
watch.Start();

            
// Old boring way!
            
foreach (string string1 in numberList)
            {
                Console.WriteLine(
"First For-Loop Value: {0}", string1);

                foreach
(string string2 in numberList)
                {
                    
int n1 Int32.Parse(string1);
                    int 
n2 Int32.Parse(string2);

                    
Console.WriteLine("\tSecond For-Loop Value: {0}+{1}={2}",
                                        n1.ToString(), n2.ToString(),
                                        (n1 + n2).ToString())
;
                
}
            }

            watch.Stop()
;
            
Console.WriteLine("For-loop time: {0} millisec(s)"
                                watch.ElapsedMilliseconds.ToString())
;

            
// One empty line for ease of read
            
Console.WriteLine();
            
watch.Reset();

            
// Get value of list without loop!
            
watch.Start();
            
numberList.ForEach(new Action<string>(
                                        
delegate(string s)
                                        {
                                            Console.WriteLine(
"First Delegate Value: {0}", s);

                                            
// Lets loop again!
                                            
numberList.ForEach(
                                                
new Action<string>(
                                                    
delegate(string s2) 
                                                    {
                                                        
// Convert to numbers
                                                        
int num1 Int32.Parse(s);
                                                        int 
num2 Int32.Parse(s2);

                                                        
// Output the simple loop sum
                                                        
Console.WriteLine("\tSecond Delegate Value: {0}+{1}={2}",
                                                                            num1.ToString(), num2.ToString(), 
                                                                            (num1 + num2).ToString())
;
                                                    
}
                                                )
                                            )
;
                                        
}
                                    )
                                )
;

            
watch.Stop();
            
Console.WriteLine("Delegate time: {0} millisec(s)"
                                watch.ElapsedMilliseconds.ToString())
;

            
watch.Reset();
            
Console.ReadLine();
        
}
    }
}