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.
Fabian
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com