Displacing an enumerable by a count What I mean by displacing an enumerable is skipping the first n items, then returning the next n items, then returning the first n items, then returning the rest of the enumerable.

What do you think?  Does this idea have any value?

using System.Collections.Generic;

public class EnumerableDisplacer
{
    public virtual IEnumerable&lt;T&gt; Displace&lt;T&gt;(IEnumerable&lt;T&gt; source, int displaceCount)
    {
        var enumerator = source.GetEnumerator();
        var counter = 0;
        var displacedItems = new LinkedList&lt;T&gt;();
        while (counter &lt; displaceCount)
        {
            enumerator.MoveNext();
            ++counter;

            displacedItems.AddLast(enumerator.Current);
        }

        counter = 0;
        while (counter &lt; displaceCount)
        {
            enumerator.MoveNext();
            ++counter;

            yield return enumerator.Current;
        }

        foreach (var di in displacedItems)
        {
            yield return di;
        }

        while (enumerator.MoveNext())
        {
            yield return enumerator.Current;
        }
    }
}
Your implementation is missing some basic validation. A public method should always validate its passed in arguments. Assume an IEnumerable&lt;int&gt; will be passed containing 10 elements 

with displaceCount == 10. You won't see an exception if it is a "true"  IEnumerables&lt;int&gt; but you will receive wrong results.

A test with  

IEnumerable&lt;int&gt; source = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
source = source.Where(i =&gt; i &gt; 0);
EnumerableDisplacer dispatcher = new EnumerableDisplacer();
var result = dispatcher.Displace(source, 10).ToList();  


will result in a List&lt;int&gt; containing the 20 elements { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }  

passing 

IEnumerable&lt;int&gt; source = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
EnumerableDisplacer dispatcher = new EnumerableDisplacer();
var result = dispatcher.Displace(source, 10).ToList();    


will result in an InvalidOperationException in the second while loop at yield return enumerator.Current; because the enumeration has ended already.  

You have two choices to do the input validation:  


checking the count of items contained in source against displacedCount which will result for a "true" IEnumerable&lt;T&gt; in iterating over the elements twice  


or  


convert the source to an collection type like e.g an array and check its Length property against displacedCount which will result in iterating over the elements twice  as well.  


I would go with the second choice because it would make the whole method easier (IMO) because you wouldn't need any other datastructure like the LinkedList.  

This would result in  

public class EnumerableDisplacer
{

    public IEnumerable&lt;T&gt; Displace&lt;T&gt;(IEnumerable&lt;T&gt; source, int displaceCount)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }

        var sourceArray = source.ToArray();
        var doubledDisplacedCount = displaceCount * 2;
        if (sourceArray.Length &lt; doubledDisplacedCount)
        {
            throw new ArgumentOutOfRangeException("source", "The source needs to contain twice as many elements than displacedCount");
        }

        return DisplaceEx&lt;T&gt;(sourceArray, displaceCount);
    }
    private IEnumerable&lt;T&gt; DisplaceEx&lt;T&gt;(IEnumerable&lt;T&gt; source, int displaceCount)
    {

        var doubledDisplacedCount = displaceCount * 2;

        for (var i = displaceCount; i &lt; doubledDisplacedCount; i++)
        {
            yield return sourceArray[i];
        }
        for (var i = 0; i &lt; displaceCount; i++)
        {
            yield return sourceArray[i];
        }
        for (var i = doubledDisplacedCount; i &lt; sourceArray.Length; i++)
        {
            yield return sourceArray[i];
        }

    }
}  




What advantage should this method get from beeing virtual ? I don't see any. Overriding this method in an inherited class doesn't make sense IMO.