Tag Archives: IEnumerator

Convert Non-Generic IEnumerator to IEnumerable

[code lang=”csharp”]
/// <summary>
/// Converts a Non-Generic IEnumerator to IEnumerable
/// </summary>
/// <param name="enumerator"></param>
/// <returns></returns>
public static IEnumerable ToIEnumerable(this IEnumerator enumerator) {
while (enumerator.MoveNext()) {
yield return enumerator.Current;
}
yield break;
}
[/code]

Convert IEnumerator to IEnumerable

[code lang=”csharp”]
/// <summary>
/// Converts an IEnumerator to an IEnumerable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumerator"></param>
/// <returns></returns>
public static IEnumerable<T> ToIEnumerable<T>(this IEnumerator<T> enumerator) {
while (enumerator.MoveNext()) {
yield return enumerator.Current;
}
yield break;
}
[/code]