Skip to content

Instantly share code, notes, and snippets.

@anderssonjohan
Created April 17, 2013 23:08
Show Gist options
  • Save anderssonjohan/5408521 to your computer and use it in GitHub Desktop.
Save anderssonjohan/5408521 to your computer and use it in GitHub Desktop.
A pretty basic chunk method I use to chunk, or split, lists of items
public static IEnumerable<T[]> Chunk<T>( this T[] source, int size )
{
var acc = new List<T>( size );
for ( var i = 0; i < source.Length; i++ )
{
acc.Add( source[i] );
if ( acc.Count < size && i < source.Length - 1 )
continue;
yield return acc.ToArray();
acc.Clear();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment