Skip to content

Instantly share code, notes, and snippets.

@wbokkers
Created August 17, 2022 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wbokkers/55780cd7620365db6f9d3dd41af350e9 to your computer and use it in GitHub Desktop.
Save wbokkers/55780cd7620365db6f9d3dd41af350e9 to your computer and use it in GitHub Desktop.
Create chunks from array
public IEnumerable<ArraySegment<T>> Chunk<T>(T[] source, int chunkSize)
{
for (int i = 0; i < source.Length; i += chunkSize)
{
var remaining = source.Length - i;
var segmentSize = Math.Min(chunkSize, remaining);
yield return new ArraySegment<T>(source, i, segmentSize);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment