I’ve recently been asked by one of our new dotNet developers whether it’s possible to cast your regular everyday .Net 1.x System.Collections.ArrayList or the like onto a generic System.Collections.Generic.List<T> . I have to admit, I seem to suffer from some kind of Obsessive-Compulsive Disorder when it comes to programming problems. I mean my original reaction is usually “Of course not, what kind of frivolous idea is that?“, but then I cannot go about without solving the problem. So it was this time.

There seems to be no pre-coded framework solution for the problem, you should iterate over the items instead.  

At first I thought we could make use of the the framework supported conversion of  List<T> into List<Y>through a somewhat awkward method in the List<T>

public List<TOutput> ConvertAll<TOutput> (
	Converter<T,TOutput> converter
)

which requires you to implement a delegate that will do the conversion for every item. One problem with this solution though is that it does something exactly oposite to what we needed. It exports rather than importing and it does so between two generic types. However, there is an easy enough way to add this functionality in and you will only need to code it once and you will probably want to place it in some kind of static utility class.

 public static void copyToGenericList<T>(IEnumerable list, IList<T> genericList)
{
    foreach (object o in list)
    {
        genericList.Add((T) o);
    }
}

With the help of this method you will copy any enumerable type into whatever generic list you may find. Granted the method is not doing any type checking and will fail in case of any type mismatch it may encounter, but you would want to know about it nonetheles, wouldn’t you? Simple try/catch it around will do the trick, either that or a simple modification: 

public static void copyToGenericListChecked<T>
    (IEnumerable list, IList<T> genericList)
{
    foreach (object o in list)
    {
        if (o is T)
        {
            genericList.Add((T)o);
        }
    }
}

Which however will fail silently if the objects in the original list are of a wrong type, which in turn makes using it quite dangerous.

The copying now becomes quite effortless:

//Create a non generic list of type in with some data in it
IList list = new ArrayList();
list.Add(456);
list.Add(123);
 
//our generic target
IList<int> intList = new List<int>();
 
//and the copy routine - completely effortless
SomeUtilClass.copyToGenericList(list, intList);

But better yet, why not simply create the list of the type you expect and populate its contents in one go from the non-generic? Sure:

public static List<T> convertToGenericList<T>(IEnumerable list)
{
    List<T> result = new List<T>();
    foreach (object o in list)
    {
        result.Add((T) o);
    }
    return result;
}

And the sample usage becomes exactly what we hoped for:

//Create a non generic list of type in with some data in it
IList list = new ArrayList();
list.Add(456);
list.Add(123);
 
//and the one line conversion - nice!
IList<int> intList2 = SomeUtilClass.convertToGenericList<int>(list);

Generics rule!

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...



This entry (Permalink) was posted on Saturday, April 14th, 2007 at 8:55 pm and is filed under .Net Framework, C#, Software Development. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.