I had a little play ...
// CodeHimBelonga kdub@theSwamp 20100914
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;
namespace ForeachTester
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
DataColumn col1 = new DataColumn("Id", typeof(int));
col1.AutoIncrement = true;
DataColumn col2 = new DataColumn("Name", typeof(string));
DataColumn col3 = new DataColumn("TimeStamp", typeof(DateTime));
dt.Columns.AddRange(new DataColumn[] { col1, col2, col3 });
dt.Rows.Add(null, "John", DateTime.Now );
dt.Rows.Add(null, "George", DateTime.Now );
dt.Rows.Add(null, "Paul", DateTime.Now );
dt.Rows.Add(null, "Ringo", DateTime.Now );
Stopwatch watch = new Stopwatch();
//For loop
watch.Start();
for (int count = 0; count < dt.Rows.Count; count++)
{
dt.Rows[count]["Name"] = "Modified in For";
}
watch.Stop();
// The smallest unit of time is the tick, which is equal to 100 nanoseconds.
Console.WriteLine("Ticks taken in For loop: {0}", watch.Elapsed.Ticks);
watch.Reset();
//Foreach loop
watch.Start();
foreach (DataRow row in dt.Rows)
{
row["Name"] = "Modified in ForEach";
}
watch.Stop();
Console.WriteLine("Ticks taken in For Each loop: {0 }", watch.Elapsed.Ticks);
Console.ReadKey();
}
}
}