Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Convert IList to List<T>

with 7 comments

Follow up with my NHibernate Series today I have another post talking about one of my favorite Util’s method.

This come from my heavy use of NHibernate classes such as DetachedCriteria, IMultiCriteria and I love to call on it .SetResultTransformer method to transform result into strong type; however its .List() method return an IList which doesn’t uncomforted me much because I addicted to generic & strong type object.

public static IList<T> ConvertToListOf<T>(IList iList)
{
  IList<T> result = new List<T>();
  foreach (T value in iList)
    result.Add(value);

  return result;
}

With this little method it allow me to work with generic type again and that is sweet ;)

EDIT: hmm, I just discovered something interesting actually I don’t really need this at all I can just manipulate iList element through foreach().

namespace ConsoleApplication1
{
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Diagnostics;

  internal class Program
  {
    private static void Main(string[] args)
    {
      IList x = new ArrayList();

      for(int i = 0; i < 100000; i++)
      {
        x.Add(new Person("First #" + i, "Last #" + i));
      }

      Stopwatch watch1 = new Stopwatch();
      watch1.Start();
      foreach(Person person in x)
      {
        Console.Write(person.FirstName);
        Console.Write(person.LastName);
      }
      watch1.Stop();

      Stopwatch watch2 = new Stopwatch();
      watch2.Start();
      IList<Person> persons = ConvertToListOf<Person>(x);
      foreach(Person person in persons)
      {
        Console.Write(person.FirstName);
        Console.Write(person.LastName);
      }
      watch2.Stop();

      Console.Clear();

      Console.WriteLine("Watch #1: {0}\r\nWatch #2: {1}", watch1.Elapsed, watch2.Elapsed);
      Console.Read();
    }

    public static IList<T> ConvertToListOf<T>(IList iList)
    {
      IList<T> result = new List<T>();
      foreach(T value in iList)
      {
        result.Add(value);
      }

      return result;
    }
  }

  public class Person
  {
    public string FirstName;
    public string LastName;

    public Person(string firstName, string lastName)
    {
      FirstName = firstName;
      LastName = lastName;
    }
  }
}

However it come down to an interesting measurement issue
* Pass through *
Watch #1: 00:00:00.0075761
Watch #2: 00:00:00.0131754

* Pass through & read field *
Watch #1: 00:00:06.2585406 (process direct)
Watch #2: 00:00:06.3542894 (convert first)

Advertisements

Written by Chorn Sokun

May 27, 2008 at 10:47 am

7 Responses

Subscribe to comments with RSS.

  1. Also – you can do the following with Linq

    var typedList = list.Cast().ToList();

    Patrick Greene

    April 16, 2009 at 7:55 pm

  2. There should be a generic type specification on the Cast but brackets were cut out… list.Cast[T]().ToList()

    Patrick Greene

    April 16, 2009 at 7:56 pm

  3. List ListTypeVar = new List(IListVar.Cast());
    // or 
    List<Tag> ListTypeVar = new List<Tag>(lListVar.Cast[Tag]());
    

    rodrijp

    June 26, 2009 at 3:02 pm

  4. List and IList are of the same type actually, as in

    both are from System.Collection.Generic

    A simple type cast will fix this.

    (List)iPersonList

    if you’re going from IList to List.

    Zac

    December 3, 2009 at 8:42 am

  5. How to convert IList to List, in case I don’t use Linq?

    Traveling to Vietnam

    January 5, 2012 at 5:02 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: