Leggere in inglese Modifica

Condividi tramite


Converter<TInput,TOutput> Delegate

Definition

Represents a method that converts an object from one type to another type.

public delegate TOutput Converter<in TInput,out TOutput>(TInput input);
public delegate TOutput Converter<TInput,TOutput>(TInput input);

Type Parameters

TInput

The type of object that is to be converted.

This type parameter is contravariant. That is, you can use either the type you specified or any type that is less derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.
TOutput

The type the input object is to be converted to.

This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.

Parameters

input
TInput

The object to convert.

Return Value

TOutput

The TOutput that represents the converted TInput.

Examples

This section contains two code examples. The first demonstrates the Converter<TInput,TOutput> delegate with the ConvertAll method of the Array class, and the second demonstrates the delegate with the ConvertAll method of the List<T> generic class.

Example 1

The following code example defines a method named PointFToPoint that converts a PointF structure to a Point structure. The example then creates an array of PointF structures, creates a Converter<PointF, Point> delegate (Converter(Of PointF, Point) in Visual Basic) to represent the PointFToPoint method, and passes the delegate to the ConvertAll method. The ConvertAll method passes each element of the input list to the PointFToPoint method and puts the converted elements into a new list of Point structures. Both lists are displayed.

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}
 */

Example 2

The following code example defines a method named PointFToPoint that converts a PointF structure to a Point structure. The example then creates a List<T> of PointF structures, creates a Converter\<PointF, Point> delegate (Converter(Of PointF, Point) in Visual Basic) to represent the PointFToPoint method, and passes the delegate to the ConvertAll method. The ConvertAll method passes each element of the input list to the PointFToPoint method and puts the converted elements into a new list of Point structures. Both lists are displayed.

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}
 */

Remarks

This delegate is used by the ConvertAll method of the Array class and the ConvertAll method of the List<T> class to convert each element of the collection from one type to another.

Extension Methods

GetMethodInfo(Delegate)

Gets an object that represents the method represented by the specified delegate.

Applies to

Prodotto Versioni
.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