iterator parameter in generics function C# -
i convertall generic function
public static iout convertall<iin, iout, t, toutput>(this iin source, converter<t, toutput> converter) iin : ienumerable<t> iout : ienumerable<toutput> { foreach (var item in source) yield return converter(item); }
the problem visual studio tell me :
the body of 'sgd.collectionextensions.convertall(iin, system.converter)' cannot iterator block because 'iout' not iterator interface type
the things have clause should recognize iterator, isn't ?
i try :
public static i<toutput> convertall<i, t, toutput>(this i<t> source, converter<t, toutput> converter) : ienumerable { foreach (var item in source) yield return converter(item); }
but says
the type parameter 'i' cannot used type arguments
so don't know do. don't want function :
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 simpler , it's working return ienumerable want same kind of iterator in output have in input
hope can me
so let's want output list
. list<t>
implements ienumerable<t>
. defining creation of new list? defining how new item added list?
you aren't.
yield return
means constructed class implement ienumerable<t>
or ienumerator<t>
. that's knows how construct. can accept input of other ienumerable<t>
. accept input of list<t>
, example. again, last function (the 1 don't like), don't need use earlier syntax functionality.
you add additional delegate parameters 2 operations need (or one, since can add generic constraint creation).
public static tout convertall<tin, tout, t, toutput>(this tin source , converter<t, toutput> converter , action<tout, toutput> adder) tin : ienumerable<t> tout : ienumerable<toutput>, new() { tout output = new tout(); foreach (var item in source) adder(output, converter(item)); return output; }
note result of you've lost deferred execution though.
Comments
Post a Comment