开发者

More elegant way to write this C# loop code?

开发者 https://www.devze.com 2023-02-04 23:33 出处:网络
I have to do these kinds of initializations all over for different members: this.Effects = new Effect [ image.Effects ];

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

精彩评论

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