Blog Stats
  • Posts - 18
  • Articles - 0
  • Comments - 14
  • Trackbacks - 82

 

Monday, July 10, 2006

Converting System.Collections to System.Collections.Generic

I believe the following is the fastest way to convert a non-typed list to a strongly typed (generic) list using C# .NET:

public List ConvertToGenericList(IList listOfObjects) {
   ArrayList notStronglyTypedList = new ArrayList(listOfObjects);
   return new List(notStronglyTypedList.ToArray(typeof(T)) as T[]);
}

Note that this will fail if the non-typed collection contains anything that cannot be casted to type of T.

Please let me know if there's a more efficient way to do this!

Billy

 

 

Copyright © Billy McCafferty