using System; using System.Collections.Generic; using System.Linq; public class Test { private static IEnumerable Swap(IEnumerable source, int smallerIndex, int greaterIndex) { using (IEnumerator e = source.GetEnumerator()) { IList saved = new List(greaterIndex-smallerIndex+1); int index = 0; while (e.MoveNext()) { // If we're outside the swapped indexes, yield return the current element if (index < smallerIndex || index > greaterIndex) { index++; yield return e.Current; } else if (index == smallerIndex) { var atSmaller = e.Current; // Save all elements starting with the current one into a list; // Continue until you find the last index, or exhaust the sequence. while (index != greaterIndex && e.MoveNext()) { saved.Add(e.Current); index++; } // Make sure we're here because we got to the greaterIndex, // not because we've exhausted the sequence if (index == greaterIndex) { // If we are OK, return the element at greaterIndex yield return e.Current; } // Enumerate the saved items for (int i = 0 ; i < saved.Count-1 ; i++) { yield return saved[i]; } // Finally, return the item at the smallerIndex yield return atSmaller; index++; } } } } public static void Main() { var data = new[] {0,1,2,3,4,5,6}; foreach (var x in Swap(data, 2, 5)) { Console.WriteLine(x); } } }