For Versus ForEach Versus Generics With Lambda

By Anthony Jackson. Filed in C#  |  
TOP del.icio.us digg

A fairly common question in programming with C#.NET is what is the best way to iterate through a collection of objects. I personally lean towards ForEach for three reasons:

  1. Syntax looks cleaner, easier to follow
  2. Removes possibility of common errors, such as out of bounds errors
  3. Makes it clear that the process is occurring on all elements in the collection

In Part 1 of Dynamic Data Access Layer we had the following code:


foreach (PropertyInfo property in properties)
{
if (property.Name == fieldName)
{
property.SetValue(instance, record[counter], null);
break;
}
}

Which using a Lambda Expression, we can simplify to two lines of code:


PropertyInfo property = properties.Single(pi => pi.Name == fieldName);
property.SetValue(instance, record[counter], null);

Of course, the foreach version will simply do nothing if the property matching the fieldName is not found, but the Single method on PropertyInfo will throw an exception, if not found. This forces us to actually choose what we wish to do in the case of their being a field which does not have a matching property.

But the interesting part of this is the Lambda expression which we’re using:


pi => pi.Name == fieldName

The easiest way to get used to this syntax is to take a look at what it would be without Lambda Expressions:


bool IsMatch(PropertyInfo pi, string fieldName)
{
return pi.Name == fieldName
}

So in the one line of code, we’re asking for each and every PropertyInfo object within the collection of properties to be passed into the function “IsMatch” and give us back only the PropertyInfo object which meets the criteria. The Lambda Expression simply gives us a very succinct way to do this in one line of code. Although it may seem like we’re sacrificing readability by doing this, once you get used to this new notation, it’s very easy to follow. Check out Lambdas, Generics and Delegates. Oh My!!! for more information on Generics and Lambdas.

As far as performance goes, I have yet to see anyone prove definitively that their is any significant difference between For, ForEach and IEnumerable Extensions with Lambda Expressions. Of course, if you are one of the few who are working on a section of code with significant performance requirements, I would strongly recommend setting up as close to real world as possible timing trials to see if you have any significant difference in performance.

About Anthony Jackson

Anthony Jackson has written 10 posts in this blog.

Anthony has been developing software professionally since 1996; working primarily with Microsoft technologies such as C#, VB.NET, ASP.NET and MS SQL Server. He has a varied background working in a number of industries, on teams of different sizes including Enterprise Application Development. He often takes on varied roles for projects as well, quickly adapting to what is needed for a given project and is involved in the full life-cycle of software development. This blog and it's articles are entirely the opinion of the author Anthony Jackson and do not reflect the opinions of his employers or clients past, present or future. He currently works for Avanade as a Senior System Analyst and loves working their, but everything written is still just his opinion and does not necessary reflect on any of his employers or clients past, present or future.

Leave a Reply

You must be logged in to post a comment.