Joan Venge
Joan Venge

Reputation: 331052

More elegant way to write this C# loop code?

I have to do these kinds of initializations all over for different members:

this.Effects = new Effect [ image.Effects ];
for ( int i = 0; i < image.NumEffects; ++i )
{
    this.Effects [ i ] = new Effect ( image.Effects [ i ] );
}

Upvotes: 2

Views: 819

Answers (3)

Emond
Emond

Reputation: 50672

Or use the Parallel.For to use multiple threads.

Upvotes: 5

Andrew Anderson
Andrew Anderson

Reputation: 3449

Linq would be something like this:

this.Effects = image.Effects.Select(x => new Effect(x)).ToArray();

Upvotes: 5

SLaks
SLaks

Reputation: 887453

Like this:

this.Effects = Array.ConvertAll(image.Effects, e => new Effect(e));

This will be faster than the equivalent LINQ calls with Select and ToArray which will probably be answered shortly after this.

Upvotes: 24

Related Questions