List<T>.ConvertAll<TOutput>(Converter<T,TOutput>) メソッド

定義

現在の List<T> の要素を別の型に変換し、変換された要素が格納されたリストを返します。

C#
public System.Collections.Generic.List<TOutput> ConvertAll<TOutput> (Converter<T,TOutput> converter);

型パラメーター

TOutput

変換後の配列要素の型。

パラメーター

converter
Converter<T,TOutput>

各要素の型を変換するための Converter<TInput,TOutput> デリゲート。

戻り値

List<TOutput>

現在の List<T> の要素の型を変換した後の List<T>

例外

converternullです。

次の例では、構造体を 構造体に変換する という名前 PointFToPointPointF メソッドを Point 定義します。 次に、構造体の PointFList<T>作成し、メソッドをConverter\<PointF, Point>表すPointFToPointデリゲート (Converter(Of PointF, Point)Visual Basic では) を作成し、デリゲートを メソッドにConvertAll渡します。 メソッドは ConvertAll 、入力リストの各要素を メソッドに PointFToPoint 渡し、変換された要素を構造体の Point 新しいリストに配置します。 両方のリストが表示されます。

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

public class Example
{
    public static void Main()
    {
        List<PointF> lpf = new List<PointF>();

        lpf.Add(new PointF(27.8F, 32.62F));
        lpf.Add(new PointF(99.3F, 147.273F));
        lpf.Add(new PointF(7.5F, 1412.2F));

        Console.WriteLine();
        foreach( PointF p in lpf )
        {
            Console.WriteLine(p);
        }

        List<Point> lp = lpf.ConvertAll(
            new Converter<PointF, Point>(PointFToPoint));

        Console.WriteLine();
        foreach( Point p in lp )
        {
            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>は、オブジェクトをターゲット型に変換するメソッドへのデリゲートです。 現在 List<T> の 要素はデリゲートに個別に渡され Converter<TInput,TOutput> 、変換された要素は新しい List<T>に保存されます。

現在 List<T> の 値は変更されません。

このメソッドは O(n) 操作で、 n は です Count

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください