Did you ever need to convert List(T1) to List(T2)? One example might be when implementing an interface. you might need to expose a collection of other interfaces (or maybe the same interface), But you usually hold the concrete type implementing the interface in the collection. Lets look at the following example:

public interface IPoint
{
    int X { get; set; }
    int Y { get; set; }
}   

public interface IPolygon
{
    IList<IPoint> Points { get; }
}

As you can expect, the Polygon contains a collection of points. Lets Look at some implementation you will probably not use – a random point class:

class RandomPoint : IPoint
{
    public RandomPoint()
    {
        x = random.Next(100);
        y = random.Next(100);
    }
    public int X
    {
        get { return x; }
        set { x = value; }
    }
    public int Y
    {
        get { return y; }
        set { y = value; }
    }
    private static Random random = new Random();
    private int x;
    private int y;
}

The random polygon implementation will contain a list of RandomPoints, but, If you try to cast it to a list of IPoint, you get a compilation error: Cannot convert type… why? because List of type A has different type than list of type B, even if B is derived from A. Many times I’ve seen code like this:

public IList<IPoint> Points
{
    get
    {
        List<IPoint> retVal = new List<IPoint>();
        foreach (RandomPoint point in points)
        {
            retVal.Add(point);
        }
        return retVal;
    }
}

Here is how you do it with the ConvertAll method of the List, and an anonymous delegate:

public IList<IPoint> Points
{
    get
    {
        return points.ConvertAll<IPoint>(
            delegate(RandomPoint point)
            {
                return point;
            });
    }   

}

The method calls the delegate for each item to convert it. I pass here an anonymous delegate where the parameter type is the type of the original list. The return type of the delegate is the type of the converted list. In this case there is nothing to do…

Note that objects of the newly created list are the same objects of the original list, but the returned list is a copy of the original list. Changes made to the new list will not affect the original one!

Tags :

6 Responses to “How to Convert List(T1) to List(T2)”


  1. Fredrik

    Said on May 15, 2008 :

    With .NET 3.5 you could also do return new List(points.Cast());

  2. Niki

    Said on May 15, 2008 :

    If RandomPoint implements the IPoint interface, you can also simple write points.Cast() (in C# 3.0) – this returns an IEnumerable that casts each item in the list to IPoint.

  3. Brian Canzanella

    Said on May 15, 2008 :

    This is great.

  4. Great Posts

    Said on November 25, 2008 :

    the posts really helped me simplify me code. a converter might have its uses but for simple type conversions its hard to beat list.Cast(Of String)

  5. Jenny

    Said on December 23, 2008 :

    sourcearticle.info has a similiar post if anyone’s interested

1 Trackback(s)

  1. May 16, 2008: Dew Drop - May 16, 2008 | Alvin Ashcraft's Morning Dew

Post a Comment