tags:

views:

127

answers:

1

if i have a class FooWrapper that takes in a Foo during construction:

Foo foo = new Foo();
FooWrapper fooWrapper = new FooWrapper(foo);

Often, i get an array of Foo's back from some API

Foo[] foos = _api.GetFoos();

is there anyway for me to build up an array of FooWrappers by passing in the appropriate Foo object without simply looping through each one here?

+8  A: 

You can use the Array.ConvertAll method.

FooWrapper[] wfoos = Array.ConvertAll<Foo, FooWrapper>(foos, delegate(Foo foo){return new FooWrapper(foo);});
arul
does this work in Visual Studio 2005 ? C# 2.0 ?
ooo
You could replace the last delegate parameter with a lambda: (f => new FooWrapper(f)). It's a matter of taste.
Henk Holterman
The Array.ConvertAll method was introduced in the Framework 2.0 so it should work in V2005. +1
Jose Basilio