Read the syntax
Divide a sequence into new arrays of a maximum size and handle a shorter final batch.
Chunk yields two full arrays and then one array containing only 5.
What it is and why to use it
A report may need groups of rows rather than one item at a time. Enumerable.Chunk, introduced in .NET 6, yields arrays containing up to the requested number of items. It is a useful batching helper, but it does not perform asynchronous work, limit service concurrency or make a database operation transactional.
How it works
The size must be positive. Enumeration is deferred, and each result is an array containing the next batch. The final array may be shorter; an empty input yields no batches. The arrays are new containers, but reference-type elements still refer to their original objects. Materialising all chunks at once also materialises their storage.
Common mistakes and limitations
Do not assume every batch has the full requested length. Do not mistake a count limit for a byte or memory limit when items vary in size. Infinite inputs and materialisation still require a deliberate resource policy.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
foreach (var batch in items.Chunk(2)) { /* process one array */ }Examples
Divide five items into batches of two
Inspect all batches including the partial final one.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
foreach (int[] batch in Enumerable.Range(1, 5).Chunk(2))
Console.WriteLine(string.Join(", ", batch));
- Range provides five sequential integers.
- Chunk yields two full arrays and then one array containing only 5.
- The loop uses each actual array length rather than indexing an assumed second element in the last batch.
- Expected output: 1, 2 3, 4 5
- Handle the partial final batch.
See that batch arrays are separate containers
Change a materialised batch without replacing an element in the original integer array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
int[] source = { 1, 2, 3 };
int[][] batches = source.Chunk(2).ToArray();
batches[0][0] = 99;
Console.WriteLine(source[0]);
Console.WriteLine(batches[0][0]);
- Chunk copied integer values into a new batch array.
- Assigning its first slot does not assign the original array’s first slot.
- For reference-type items the objects themselves could still be shared; new containers are not a deep-copy guarantee.
- Expected output: 1 99
- Handle the partial final batch.
Best practices
- Handle the partial final batch.
- Choose batch size and concurrency as separate decisions.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference