This post will discuss how to split an array into chunks of a specific size in C#.

1. Using Skip() and Take()

The Take() method returns a specified number of elements from the beginning of a sequence, and the Skip() method skips the specified number of elements in a sequence. They can be used as follows to split an array into chunks of smaller arrays of a specific size.

Download  Run Code

Output:

1, 2
3, 4
5

 
Another option is to use LINQ’s Select() method to split an array into equal length subarrays. The following code example shows how to use this with Skip() and Take() method:

Download  Run Code

Output:

1, 2
3, 4
5

2. Using Enumerable.GroupBy Method

Alternatively, you can use the LINQ’s Enumerable.GroupBy method to group all items by the chunk size and convert each sequence to a new array. Here’s an example of its usage:

Download  Run Code

Output:

1, 2
3, 4
5

That’s all about splitting an array into chunks of a specific size in C#.