using System.Collections.Generic;
public static void Main(string[] args)
values = new[] { "a", "b", "c" };
separators = new[] { "1", "2" };
Console.WriteLine(string.Join("", values.Interleave(separators)));
values = new[] { "a", "b" };
separators = new[] { "1", "2", "3", "4", "5", "6" };
Console.WriteLine(string.Join("", values.Interleave(separators)));
values = new[] { "a", "b", "c", "d", "e", "f" };
separators = new[] { "1", "2" };
Console.WriteLine(string.Join("", values.Interleave(separators)));
public static class EnumerableExtensions
public static IEnumerable<TSource> Interleave<TSource>(this IEnumerable<TSource> source1, IEnumerable<TSource> source2)
if (source1 == null) { throw new ArgumentNullException(nameof(source1)); }
if (source2 == null) { throw new ArgumentNullException(nameof(source2)); }
using (var enumerator1 = source1.GetEnumerator())
using (var enumerator2 = source2.GetEnumerator())
if (continue1 = enumerator1.MoveNext())
yield return enumerator1.Current;
if (continue2 = enumerator2.MoveNext())
yield return enumerator2.Current;
while (continue1 || continue2);