Generic method to split provided collection into smaller collections First time writing a generic method here.  Input a List&lt;T&gt; and an int value and output a List&lt;List&lt;T&gt;&gt; with each member List&lt;T&gt; of the provided int input.

No doubt there are easier / less verbose ways of handling this via LINQ but I wanted to try my hand with a generic method.  Please let me know of any ways that I could improve this method.

static List&lt;List&lt;T&gt;&gt; SplitIntoChunks&lt;T&gt;(List&lt;T&gt; fullBatch, int chunkSize)
{
    if (chunkSize &lt;= 0)
    {
        throw new ArgumentOutOfRangeException("Chunk size cannot be less than or equal to zero.");
    }

    if (fullBatch == null)
    {
        throw new ArgumentNullException("Input to be split cannot be null.");
    }

    int numOfChunks = fullBatch.Count / chunkSize;

    //handles uneven number of items within the full batch to ensure none at the end are missed
    if (fullBatch.Count % chunkSize &gt; 0)
    {
        numOfChunks++;
    }

    int cellCounter = 0;

    List&lt;List&lt;T&gt;&gt; splitChunks = new List&lt;List&lt;T&gt;&gt;();

    for (int chunkNum = 0; chunkNum &lt; numOfChunks; chunkNum++)
    {
        var chunk = new List&lt;T&gt;();
        for (int index = 0; index &lt; chunkSize; index++)
        {
            if (index &lt; fullBatch.Count)
            {
                chunk.Add(fullBatch[index]);
                cellCounter++;
            }
        }
        splitChunks.Add(chunk);
    }

    return splitChunks;
}
Your implementation is not bad for a no-LinQ solution. But there's always room for improvement. First I'll provide a LinQ solution that provides a clean way to return a chunked list:

public static List&lt;List&lt;T&gt;&gt; Split&lt;T&gt;(List&lt;T&gt; collection, int size)
{
    var chunks = new List&lt;List&lt;T&gt;&gt;();
    var chunkCount = collection.Count() / size;

    if (collection.Count % size &gt; 0)
        chunkCount++;

    for (var i = 0; i &lt; chunkCount; i++)
        chunks.Add(collection.Skip(i * size).Take(size).ToList());

    return chunks;
}


Basically it comes down to this:


calculate the count of chunks that are needed
loop over the length of chunks
use the Enumerable.Skip and Enumerable.Take methods to get chunks
return the list of chunks


Now, you implemented a no-LinQ solution so I created one myself too. My implementation doesn't have to calculate the amount of chunks or use two loops to create the list of chunks:

public static List&lt;List&lt;T&gt;&gt; SplitNoLinq&lt;T&gt;(List&lt;T&gt; collection, int size)
{
    var chunks = new List&lt;List&lt;T&gt;&gt;();
    var count = 0;
    var temp = new List&lt;T&gt;();

    foreach (var element in collection)
    {
        if (count++ == size)
        {
            chunks.Add(temp);
            temp = new List&lt;T&gt;();
            count = 1;
        }
        temp.Add(element);
    }

    chunks.Add(temp);  
    return chunks;
}


The code iterates over the collection and keeps a counter, adding the iterated item to a temporary list. If the counter equals the desired length of a chunk it will add the temporary list to the return list. At the end, the last chunk is added.



The var keyword:

From the C# Programming Guide:


  The var keyword can also be useful when the specific type of the variable is tedious to type on the keyboard, or is obvious, or does not add to the readability of the code.


So lines like:

List&lt;List&lt;T&gt;&gt; splitChunks = new List&lt;List&lt;T&gt;&gt;();


would become:

var splitChunks = new List&lt;List&lt;T&gt;&gt;();




Furthermore you could place the code in an extension method, also using an IEnumerable&lt;T&gt; instead of List&lt;T&gt;:

public static class Extensions
{
    public static List&lt;List&lt;T&gt;&gt; Split&lt;T&gt;(this IEnumerable&lt;T&gt; collection, int size)
    {
        var chunks = new List&lt;List&lt;T&gt;&gt;();
        var count = 0;
        var temp = new List&lt;T&gt;();

        foreach (var element in collection)
        {
            if (count++ == size)
            {
                chunks.Add(temp);
                temp = new List&lt;T&gt;();
                count = 1;
            }
            temp.Add(element);
        }
        chunks.Add(temp);

        return chunks;
    }
}

//USAGE::
var numbers = new List&lt;int&gt; { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
var chunked = numbers.Split(5);