iterator parameter in generics function C#
I would like to do a convertAll generic a function like
public static Iout ConvertAll<Iin, Iout, T, TOutput>(this Iin source,
Converter<T, TOutput> converter)
where Iin : IEnumerable<T>
where Iout : IEnumerable<TOutput>
{
foreach (var item in source)
yield return converter(item);
}
The problem is that visual studio tell me :
The body of 'Sgd.CollectionExtensions.ConvertAll(Iin, System.Converter)'
cannot be an iterator block because 'Iout' is not an iterator interface
type
the things is I have the where clause so it should be recognize like an
iterator, isn't ?
i also try :
public static I<TOutput> ConvertAll<I, T, TOutput>(this I<T> source,
Converter<T, TOutput> converter)
where I : IEnumerable
{
foreach (var item in source)
yield return converter(item);
}
but it says
The type parameter 'I' cannot be used with type arguments
so i don't know what to do. i don't want a function like :
public static IEnumerable<TOutput> ConvertAll<T, TOutput>(this
IEnumerable<T> source, Converter<T, TOutput> converter)
{
foreach (var item in source)
yield return converter(item);
}
it's much simpler and it's working but it return a IEnumerable an i want
the same kind of iterator in output that i have in input
Hope you can help me Thanks
No comments:
Post a Comment