As Fabian says, if it's likely that your IList is just pointing to a List<T>, then casting would be most performant.
Otherwise, List<T> is just a wrapper for an array. List<T>(IEnumerable<T> enumerable) simply checks is enumerable is a collection and if it is simply calls ICollection<T>.CopyTo to copy the collection to a new array. Depending on how your using List<T> (i.e. not modifying it), you might find that using an array to be a bit quicker. On Thu, 5 Apr 2007 13:35:03 +0200, Fabian Schmied <[EMAIL PROTECTED]> wrote: >> Is there a better way to cast an IList to List other than how I am doing it here: >> >> get >> { >> IList<IRecipeStep> result = (_recipeMode == Mode.Temp) ? _recipeUsersDictionary[AdminUserUid].RecipeSteps : _liveRecipe.RecipeSteps; >> PersistentGenericBag<IRecipeStep> bag = (PersistentGenericBag<IRecipeStep>)result; >> return new List<IRecipeStep>((IEnumerable<IRecipeStep>)bag); >> } > >If you suspect that the IList<T> might be an implementation of List<T> >(rather than an array or something), you can just try to cast it; >otherwise, you'll have to construct a new List<T> and copy the source, >as you do it > > I.e.: > >public static List<T> MakeList<T>(IList<T> sourceList) >{ > List<T> resultList = sourceList as List<T>; > if (resultList == null) > { > resultList = new List<T> (sourceList); > } > return resultList; >} > >However, constructing a new list like that might be a lengthy >operation, depending on how long the source list is. It's therefore >questionable if one should do it in a property getter - property >getters should be quite fast, more like a field access than like a >longer calculation. > >> I prefer to work with an List instead of an IList so I can use the functional list processing with annonymous delegates. > >Yes, it's a real shame that these algorithms are only implemented on >List<T> and on Array in the .NET framework. You can use the free >Wintellect PowerCollections, though, which implement them (and many >more) for the most general IEnumerable<T>. Then you wouldn't need to >cast/copy any longer. http://discuss.develop.com =================================== This list is hosted by DevelopMentorĀ® http://www.develop.com View archives and manage your subscription(s) at http://discuss.develop.com