Skip to content

Instantly share code, notes, and snippets.

@fearthecowboy
Created June 16, 2014 15:24
Show Gist options
  • Save fearthecowboy/04faae5610af41a1d93d to your computer and use it in GitHub Desktop.
Save fearthecowboy/04faae5610af41a1d93d to your computer and use it in GitHub Desktop.
Handy IEnumerator<T> extension methods
public static IEnumerable<T> ToIEnumerable<T>(this IEnumerator<T> enumerator) {
if (enumerator != null) {
while (enumerator.MoveNext()) {
yield return enumerator.Current;
}
}
}
public static T[] ToArray<T>(this IEnumerator<T> enumerator) {
return ToIEnumerable<T>(enumerator).ToArray();
}
public static IEnumerable<T> Concat<T> (this IEnumerator<T> set1, IEnumerator<T> set2) {
IEnumerable<T> s1 = set1 == null ? Enumerable.Empty<T>() : set1.ToIEnumerable();
IEnumerable<T> s2 = set2 == null ? Enumerable.Empty<T>() : set2.ToIEnumerable();
return s1.Concat(s2);
}
public static IEnumerable<T> Concat<T>(this IEnumerable<T> set1, IEnumerator<T> set2) {
IEnumerable<T> s1 = set1 ?? Enumerable.Empty<T>();
IEnumerable<T> s2 = set2 == null ? Enumerable.Empty<T>() : set2.ToIEnumerable();
return s1.Concat(s2);
}
public static IEnumerable<T> Concat<T>(this IEnumerator<T> set1, IEnumerable<T> set2) {
IEnumerable<T> s1 = set1 == null ? Enumerable.Empty<T>() : set1.ToIEnumerable();
IEnumerable<T> s2 = set2 ?? Enumerable.Empty<T>();
return s1.Concat(s2);
}
public static T FirstOrDefault<T>(this IEnumerator<T> set) {
return set.ToIEnumerable().FirstOrDefault();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment