C# Array ConvertAll

Description

Array ConvertAllconverts an array of one type to an array of another type.

Syntax

Array.ConvertAll has the following syntax.


public static TOutput[] ConvertAll<TInput, TOutput>(
  TInput[] array,
  Converter<TInput, TOutput> converter
)

Parameters

Array.ConvertAll has the following parameters.

  • TInput - The type of the elements of the source array.
  • TOutput - The type of the elements of the target array.
  • array - The one-dimensional, zero-based Array to convert to a target type.
  • converter - A Converter that converts each element from one type to another type.

Returns

Array.ConvertAll method returns

Example

The following code example defines a method named PointFToPoint that converts a PointF structure to a Point structure.


using System;//from   www .  jav a  2 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 »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version