Utilizing Chunk to separate an array of integers in C#
Allow us to perceive this with a code instance. Take into account the next code, which makes use of the Chunk extension methodology to divide an array of integers into chunks of equal sizes.
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
var chunks = numbers.Chunk(5);
int counter = 0;
foreach (var chunk in chunks)
{
Console.WriteLine($"Chunk #{++counter}");
Console.WriteLine(string.Be part of(", ", chunk));
}
Within the previous code instance, we create an array of 15 integers, then use the Chunk methodology to separate the array into chunks of equal sizes, i.e., 5 components on this instance. Lastly, we show the integers contained in every chunk on the console.