英語で読む

次の方法で共有


Array.ConvertAll<TInput,TOutput> メソッド

定義

ある型の配列を別の型の配列に変換します。

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

型パラメーター

TInput

元の配列要素の型。

TOutput

変換後の配列要素の型。

パラメーター

array
TInput[]

変換元となる、インデックス番号が 0 から始まる 1 次元 Array

converter
Converter<TInput,TOutput>

各要素の型を変換するための Converter<TInput,TOutput>

戻り値

TOutput[]

要素の型を変換した後の配列。

例外

arraynullです。

または

converternullです。

次のコード例では、構造体を構造体に変換する名前 PointFToPointPointF メソッドを Point 定義します。 次に、構造体のPointF配列を作成し、メソッドをConverter<PointF, Point>表すPointFToPointデリゲート (Converter(Of PointF, Point)Visual Basic) を作成し、デリゲートをメソッドにConvertAll渡します。 このメソッドは ConvertAll 、入力リストの各要素をメソッドに PointFToPoint 渡し、変換された要素を新しい構造体の Point リストに配置します。 F# の例では、 pointFToPoint 関数は暗黙的にデリゲートに Converter<PointF, Point> キャストされます。 両方のリストが表示されます。

C#
using System;
using System.Drawing;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create an array of PointF objects.
        PointF[] apf = {
            new PointF(27.8F, 32.62F),
            new PointF(99.3F, 147.273F),
            new PointF(7.5F, 1412.2F) };

        // Display each element in the PointF array.
        Console.WriteLine();
        foreach( PointF p in apf )
            Console.WriteLine(p);

        // Convert each PointF element to a Point object.
        Point[] ap = Array.ConvertAll(apf,
            new Converter<PointF, Point>(PointFToPoint));

        // Display each element in the Point array.
        Console.WriteLine();
        foreach( Point p in ap )
        {
            Console.WriteLine(p);
        }
    }

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

/* This code example produces the following output:

{X=27.8, Y=32.62}
{X=99.3, Y=147.273}
{X=7.5, Y=1412.2}

{X=27,Y=32}
{X=99,Y=147}
{X=7,Y=1412}
 */

注釈

オブジェクト Converter<TInput,TOutput> をターゲット型に変換するメソッドへのデリゲートです。 の array 要素は個別に渡され Converter<TInput,TOutput>、変換された要素は新しい配列に保存されます。

ソース array は変更されません。

このメソッドは O(n) 演算で、次narray値を指定しますLength

F# では、 通常、代わりに Array.map 関数が使用されます。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

こちらもご覧ください