6
votes

I don't have much experience with using the yield keyword. I have these IEnumerable<T> extensions for Type Conversion.

My question is does the first overloaded method have the same yield return effect that I'm getting from the second method?

public static IEnumerable<TTo> ConvertFrom<TTo, TFrom>(this IEnumerable<TFrom> toList)
{
    return ConvertFrom<TTo, TFrom>(toList, TypeDescriptor.GetConverter(typeof(TTo)));
}

public static IEnumerable<TTo> ConvertFrom<TTo, TFrom>(this IEnumerable<TFrom> toList, TypeConverter converter)
{
    foreach (var t in toList)
        yield return (TTo)converter.ConvertFrom(t);
}
3
I'll update re your comment to Jon...Marc Gravell
Please, set up your comment as an answer in this thred stackoverflow.com/questions/1947919/… and I mark it accepted.A1exandr Belan

3 Answers

4
votes

When you call the first overload, it will immediately call the second overload. That won't execute any of the code in its body, which will have been moved into a nested class implementing the iterator block. When GetEnumerator() and then MoveNext() are first called on the returned IEnumerable<TTo>, then the code in your second overload will begin executing.

I have a fairly long article on the iterator block implementation, which you may find interesting.

2
votes

Yes, because yield return just generates an IEnumerator class on compile. yield return is just compiler magic.

2
votes

As an aside... most type-converters only work to/from strings. Another interesting option here might be the static conversion operators defined against the type(s) - I have some .NET 3.5 code that does this in MiscUtil - see the Convert method mentioned here.

Re your comment:

the way I'm using this method is to translate an EF object into a DataContract object for WCF. so List(Hotels).ConvertTo(SerializableHotels, Hotels)()

It sounds like you should either be using serialization, or if the property names have a direct relationship, maybe something like PropertyCopy<To>.CopyFrom(from) - this is again from MiscUtil (but some of Jon's work this time) - i.e.

public static IEnumerable<TTo> ConvertFrom<TTo, TFrom>(
    this IEnumerable<TFrom> list, TypeConverter converter)
{
    return list.Select(item => PropertyCopy<TTo>.CopyFrom<TFrom>(item);
}

Other than that, you're probably better-off talking about a Conversion<,> rather than a TypeConverter.