Convert an array of one type to an array of another type in CSharp

Description

The following code shows how to convert an array of one type to an array of another type.

Example


using System;//from   ww w.j  a v a2 s .  c  o m
using System.Drawing;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        PointF[] apf = {
            new PointF(7.8F, 3.2F),
            new PointF(9.3F, 7.73F),
            new PointF(7.5F, 2.2F) };

        Point[] ap = Array.ConvertAll(apf, new Converter<PointF, Point>(PointFToPoint));
        foreach( Point p in ap )
        {
            Console.WriteLine(p);
        }
    }

    public static Point PointFToPoint(PointF pf)
    {
        return new Point(((int) pf.X), ((int) pf.Y));
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var