---
title: "Next generation iteration: Array.ForEach and Delegate inference"
url: "https://osherove.com/blog/2004/12/30/next-generation-iteration-arrayforeach-and-delegate-inferenc.md"
canonical: "https://osherove.com/blog/2004/12/30/next-generation-iteration-arrayforeach-and-delegate-inferenc.html"
published: "2004-12-30T06:46:00"
updated: "2004-12-30T06:46:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "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:…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Next generation iteration: Array.ForEach and Delegate inference

By Roy Osherove December 30, 2004 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published December 30, 2004; technical details may be outdated.

[Sriram goes through hoops](http://dotnetjunkies.com/WebLog/sriram/archive/2004/12/27/39119.aspx) 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](http://www.cincomsmalltalk.com/blog/blogView?showComments=true&entry=3281617728), 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.

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Jan 5, 2005

    ### [A simple question about overloading](https://osherove.com/blog/2005/1/5/a-simple-question-about-overloading.html)

- Dec 31, 2004

    ### [Manage many events easily with EventHandlerList](https://osherove.com/blog/2004/12/31/manage-many-events-easily-with-eventhandlerlist.html)

- Dec 29, 2004

    ### [Do not serialize delegates and events blindly](https://osherove.com/blog/2004/12/29/do-not-serialize-delegates-and-events-blindly.html)

- Dec 29, 2004

    ### [Denying your public classes from your end customers](https://osherove.com/blog/2004/12/29/denying-your-public-classes-from-your-end-customers.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
