By

ForEach and generics in C#

Working on toepoke today and I wanted to loop through a list of objects and sync up some properties, so I started off by looping through a normal foreach construct. I vaguely remember there was a ForEach method hanging off the intellisense for list objects, so a quick google revealed … not much - granted I didn’t do a extensive search … I’m very much in the Not on the first page … I’ll figure it out myself! camp ;D

Anyway seems like a good opportunity for a blog. We’ll just use a nice simple Person object with the following properties for illustration:

  • Name - useful for seeing what’s going on
  • DOB - which we’ll generate when create a list of Person
  • Age - which is the property we’re going to loop around and calculate.

Which gives us the following noddy class:

public class Person {
  public string Name { get; set; }
  public DateTime DOB { get; set; }
  public int Age { get; set; }
}

We’re going to drive this example through a unit test (see full exert below), but I’ll just concentrate on the relevant bits here.  So next up let’s populate a list of people to create our test data.

List<Person> people = new List<Person>() {
  new Person() { 
    Name = "Fred Flintstone", 
    DOB = DateTime.Now.AddYears(-65); 
  },
  new Person() { 
    Name = "Homer Simpson", 
    DOB = DateTime.Now.AddYears(-40); 
  }
}

Simple enough, couple of cases, one aged 65, the other aged 40.  Our simple ForEach will loop through and work out the age of each instance.  To do this we’ll just take the DOB away from the current time to delivery the number of years.  The ForEach method is simply a delegate (which in essence is just a function) which may or may not take any arguments.  Ours isn’t taking any arguments (it’s a simple example after all :-)).  The looks like this:

people.ForEach(
  p => {
    p.Age = (DateTime.Now - p.DOB).TotalYears();
  }
);

Now we’re working against a list of type Person which is the person => bit.  The right hand side inside the curly brackets is in basically a method created on the fly for the purposes of the ForEach method. 

So in essence the above is saying for each person in the list of person set the age to the total number of years between the person’s date of birth and now.  Doesn’t look to scary now does it :-)?

The more observant of you will have noticed that TotalYears doesn’t exist in a TimeSpan object.  And you’d be absolutely correct; sadly the TimeSpan object doesn’t provide a TotalYears method because it’s quite complex to work out due to leap years and … erm … other stuff that’s hard to work out ;-).  So to help out with our example, it’s time for a little extension method on the TimeSpan class:

public static class TimeSpan_Extensions {
	/// <summary>
	/// This method is for demonstration purposes only.
	/// I _do_not_ recommend using this method for any production code whatsoever; I will guarantee bugs!
	/// </summary>
  public static int TotalYears(this TimeSpan ts) {
		return (int) ts.TotalDays / 365;
	}
}

I would recommend you don’t use that method for any production code whatsoever, I will guarantee bugs!  But it’s perfect for our purposes.  Hopefully you’ve found this useful, and the full source is posted below, good luck and good searching.

Posted in : code, generics

Written by
Founder, Developer, tea maker