开发者

C#: delegate with generic

Please focus on

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

inside the codes below.

Converter<PointF, Point> holds two type parameters? Why PointFToPoint just hold one parameter?

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>(PointFTo开发者_JAVA百科Point));

        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));
    }
}


" Converter holds two type parameters? How do I know the parameters of the method being passed on to the constructor of Converter()? "

This is how converter delegate is defined. Converter holds two type parameters? How do I know the parameters of the

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

As soon as you create instance of this delegate by passing a method which abides with this signature (accepting value of one type and converting it into value of another type), you define the parameter of the method also.

So, my answer is while creating this converter you very well know the concrete types for the generic Converter method, and the Type of the method parameter as well.


Well, in fact you have only one parameter that is passed to the converter who's output type is the return type of you converter and input type is the input type of your argument and the instance is the argument itself.


I'm not quite sure what you are asking here. But the expression can be written like this:

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

Where p is the point you want to convert. I'm not sure this helps you in any way, but it might be a little bit clearer what it does.

Update

This: <PointF, Point> doesn't mean that the method takes two parameters. It means that it should take one parameter (type PointF) and return a object of type Point.


Adding to the other answers, to shorten things up you can also write:

List<Point> lp = lpf.ConvertAll(PointFToPoint);

If you don't need that PointFToPoint method in another place, you can also remove the whole public static Point PointFToPoint(PointF pf) method and use an inline delegate instead:

List<Point> lp = lpf.ConvertAll((delegate PointF pf)
    { return new Point(((int) pf.X), ((int) pf.Y)); });

And if you're in .NET 3.5 you can shorten that up with a lambda:

List<Point> lp = lpf.ConvertAll(pf => new Point(((int) pf.X), ((int) pf.Y)));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜