C# Cast Entire Array?

前端 未结 4 1088
鱼传尺愫
鱼传尺愫 2020-11-30 09:43

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

4条回答
  •  余生分开走
    2020-11-30 10:16

    Cast doesn't consider user defined implicit conversions so you can't cast the array like that. You can use select instead:

    myArray.Select(p => (Vec2)p).ToArray();
    

    Or write a converter:

    Array.ConvertAll(points, (p => (Vec2)p));
    

    The latter is probably more efficient as the size of the result is known in advance.

提交回复
热议问题