I see this Array.ConvertAll method, but it requires a Converter as an argument. I don’t see why I need a converter, when I’ve already defined an implicit one in my class:
public static implicit operator Vec2(PointF p)
{
return new Vec2(p.X, p.Y);
}
I’m trying to cast an array of PointFs to an array of Vec2s. Is there a nice way to do this? Or should I just suck it up and write (another) converter or loop over the elements?
The proposed LINQ solution using
Cast/’Select’ is fine, but since you know you are working with an array here, usingConvertAllis rather more efficienct, and just as simple.Using
ConvertAllmeansa) the array is only iterated over once,
b) the operation is more optimised for arrays (does not use
IEnumerator<T>).Don’t let the
Converter<TInput, TOutput>type confuse you – it is just a simple delegate, and thus you can pass a lambda expression for it, as shown above.