开发者

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 ( imag开发者_StackOverflowe.Effects [ i ] );
}


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.


Linq would be something like this:

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


Or use the Parallel.For to use multiple threads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜