Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« Is PostXing gonna solve my blogging needs? | Main | Comparing C++ unit testing frameworks »
Thursday
Dec302004

Next generation iteration: Array.ForEach and Delegate inference

Sriram goes through hoops trying to write a function that maps a list to a function pointer that will be called on each item of the list. Yeah, it's easier in Smalltalk, today. But here's how it looks in .Net 2.0:

Person[] MyArray = new Person[]{..};

Array.ForEach(MyArray,MyFunction);

private void MyFunction(Person p)

{

//do something

}

 

How "Perl"ish :)

A few new things here:

  1. A whole bunch of new methods has been added to the BCL with support for automatic iterations such as Array.ForEach()
  2. You don't have to create a delegate just to point at a specific function. The compiler "resolves" this for you and infers the correct delegate type to create form the function you point to.
  3. The function just needs to match the loop variable.

Very cool.

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>