I recently run into trouble when trying to AddRange(IEnumerable) to a List. Probably a classic issue, but I do not really get it yet.
I understand that methods expec
The cast solution of course might generated class cast exceptions. The person who posted the enumerable extension work around said it was awkward. I came up with a solution which is only half as awkward, don't know if I'll use it:
public static class UpTo
{
public static IEnumerable From(IEnumerable source) where F:T
{
// this cast is guaranteed to work
return source.Select(f => (T) f);
}
}
Usage:
IEnumerable mammals = UpTo