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

定義

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

public:
generic <typename TOutput>
 System::Collections::Generic::List<TOutput> ^ ConvertAll(Converter<T, TOutput> ^ converter);
public System.Collections.Generic.List<TOutput> ConvertAll<TOutput>(Converter<T,TOutput> converter);
member this.ConvertAll : Converter<'T, 'Output> -> System.Collections.Generic.List<'Output>
Public Function ConvertAll(Of TOutput) (converter As Converter(Of T, TOutput)) As List(Of TOutput)

型パラメーター

TOutput

ターゲット配列の要素の型。

パラメーター

converter
Converter<T,TOutput>

各要素を 1 つの型から別の型に変換する Converter<TInput,TOutput> デリゲート。

返品

List<TOutput>

現在のList<T>から変換された要素を含むターゲット型のList<T>

例外

converternullです。

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

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}
 */
Imports System.Drawing
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim lpf As New List(Of PointF)

        lpf.Add(New PointF(27.8, 32.62))
        lpf.Add(New PointF(99.3, 147.273))
        lpf.Add(New PointF(7.5, 1412.2))

        Console.WriteLine()
        For Each p As PointF In lpf
            Console.WriteLine(p)
        Next

        Dim lp As List(Of Point) = lpf.ConvertAll( _
            New Converter(Of PointF, Point)(AddressOf PointFToPoint))

        Console.WriteLine()
        For Each p As Point In lp
            Console.WriteLine(p)
        Next

    End Sub

    Public Shared Function PointFToPoint(ByVal pf As PointF) _
        As Point

        Return New Point(CInt(pf.X), CInt(pf.Y))
    End Function
End Class

' 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=28,Y=33}
'{X=99,Y=147}
'{X=8,Y=1412}

注釈

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

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

このメソッドは O(n) 演算であり、 nCount

適用対象

こちらもご覧ください