Method Buffer
| Edit this pageBuffer<TSource>(IEnumerable<TSource>, int)
Generates a sequence of non-overlapping adjacent buffers over the source sequence.
Declaration
public static IEnumerable<IList<TSource>> Buffer<TSource>(this IEnumerable<TSource> source, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | Source sequence. |
| int | count | Number of elements for allocated buffers. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IList<TSource>> | Sequence of buffers containing source sequence elements. |
Type Parameters
| Name | Description |
|---|---|
| TSource | Source sequence element type. |
Remarks
A chunk can contain fewer elements than count, specifically the final buffer of source.
This method is a synonym for Batch<TSource>(IEnumerable<TSource>, int).
Returned subsequences are buffered, but the overall operation is streamed.
Examples
The following code example demonstrates how to buffer a sequence using Buffer.
var sequence = Enumerable.Range(1, 10);
// Break the sequence of numbers into three chunks of 3 and one chunk of 1
var result = sequence.Buffer(3);
Console.WriteLine(
"[" +
string.Join(
", ",
result.Select(c => "[" + string.Join(", ", c) + "]")) +
"]");
// This code produces the following output:
// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
Buffer<TSource>(IEnumerable<TSource>, int, int)
Generates a sequence of buffers over the source sequence, with specified length and possible overlap.
Declaration
public static IEnumerable<IList<TSource>> Buffer<TSource>(this IEnumerable<TSource> source, int count, int skip)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<TSource> | source | Source sequence. |
| int | count | Number of elements for allocated buffers. |
| int | skip | Number of elements to skip between the start of consecutive buffers. |
Returns
| Type | Description |
|---|---|
| IEnumerable<IList<TSource>> | Sequence of buffers containing source sequence elements. |
Type Parameters
| Name | Description |
|---|---|
| TSource | Source sequence element type. |
Remarks
A chunk can contain fewer elements than count, specifically the final buffer(s) of
source.
Returned subsequences are buffered, but the overall operation is streamed.
Examples
The following code example demonstrates how to buffer a sequence using Buffer.
var sequence = Enumerable.Range(1, 10);
// Break the sequence of numbers into overlapping chunks of size 3
var result = sequence.Buffer(3, 2);
Console.WriteLine(
"[" +
string.Join(
", ",
result.Select(c => "[" + string.Join(", ", c) + "]")) +
"]");
// This code produces the following output:
// [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]]
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|