Fastest Convert from Collection to List<T>
What version of the framework? With 3.5 you could presumably use:
List<ManagementObject> managementList = managementObjects.Cast<ManagementObject>().ToList();
(edited to remove simpler version; I checked and ManagementObjectCollection
only implements the non-generic IEnumerable
form)
You could use
using System.Linq;
That will give you a ToList<> extension method for ICollection<>
managementObjects.Cast<ManagementBaseObject>().ToList();
is a good choice.
You could improve performance by pre-initialising the list capacity:
public static class Helpers
{
public static List<T> CollectionToList<T>(this System.Collections.ICollection other)
{
var output = new List<T>(other.Count);
output.AddRange(other.Cast<T>());
return output;
}
}