Array.ConvertAll

https://msdn.microsoft.com/ko-kr/library/kt456a2y(v=vs.110).aspx

public delegate TOutput Converter<in TInput, out TOutput>(
TInput input
)

 static System.Drawing.Point Point2DrawingPoint(Point p)
{
     return (System.Drawing.Point)p;
}

static void Main(string[] args)
{

List<Point> Vertices = new List<Point>()
{
new Point() { X = 400, Y = 10 },
new Point() { X = 300, Y = 210 },
new Point() { X = 500, Y = 210 }
};

// 1. Lambda expression
System.Drawing.Point[] points1 = Array.ConvertAll(Vertices.ToArray(), x => (System.Drawing.Point)x); // Use with Point.cs type conversion operator overload

// 2. Anonymous method
System.Drawing.Point[] points2 = Array.ConvertAll(Vertices.ToArray(), delegate(Point x) { return (System.Drawing.Point)x; });

// 3. Delegate (Converter<TInput, TOutput>)
System.Drawing.Point[] points3 = Array.ConvertAll(Vertices.ToArray(), new Converter<Point, System.Drawing.Point>(Point2DrawingPoint));

foreach (var v in Vertices)
Console.WriteLine(v);
foreach (var p in points1)
Console.WriteLine(p);
foreach (var p in points2)
Console.WriteLine(p);
foreach (var p in points3)
Console.WriteLine(p);

}

Leave a Reply

Your email address will not be published. Required fields are marked *