Method Fold
| Edit this page View SourceFold<T, TResult>(IEnumerable<T>, Func<T, TResult>)
Returns the result of applying a function to a sequence of 1 element.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 1 element using Fold
.
var sequence = Enumerable.Range(1, 1);
// Fold a sequence into a single value.
var result = sequence
.Fold((a) => a);
Console.WriteLine(result);
// This code produces the following output:
// 1
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, TResult>)
Returns the result of applying a function to a sequence of 2 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 2 elements using Fold
.
var sequence = Enumerable.Range(1, 2);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b) => a + b);
Console.WriteLine(result);
// This code produces the following output:
// 3
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, TResult>)
Returns the result of applying a function to a sequence of 3 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 3 elements using Fold
.
var sequence = Enumerable.Range(1, 3);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c) => a + b + c);
Console.WriteLine(result);
// This code produces the following output:
// 6
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 4 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 4 elements using Fold
.
var sequence = Enumerable.Range(1, 4);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d) => a + b + c + d);
Console.WriteLine(result);
// This code produces the following output:
// 10
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 5 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 5 elements using Fold
.
var sequence = Enumerable.Range(1, 5);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e) => a + b + c + d + e);
Console.WriteLine(result);
// This code produces the following output:
// 15
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 6 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 6 elements using Fold
.
var sequence = Enumerable.Range(1, 6);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f) => a + b + c + d + e + f);
Console.WriteLine(result);
// This code produces the following output:
// 21
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 7 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 7 elements using Fold
.
var sequence = Enumerable.Range(1, 7);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g) =>
a + b + c + d + e + f + g);
Console.WriteLine(result);
// This code produces the following output:
// 28
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 8 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 8 elements using Fold
.
var sequence = Enumerable.Range(1, 8);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h) =>
a + b + c + d + e + f + g + h);
Console.WriteLine(result);
// This code produces the following output:
// 36
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 9 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 9 elements using Fold
.
var sequence = Enumerable.Range(1, 9);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h, i) =>
a + b + c + d + e + f + g + h + i);
Console.WriteLine(result);
// This code produces the following output:
// 45
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 10 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 10 elements using Fold
.
var sequence = Enumerable.Range(1, 10);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h, i, j) =>
a + b + c + d + e + f + g + h + i + j);
Console.WriteLine(result);
// This code produces the following output:
// 55
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 11 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 11 elements using Fold
.
var sequence = Enumerable.Range(1, 11);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h, i, j, k) =>
a + b + c + d + e + f + g + h + i + j + k);
Console.WriteLine(result);
// This code produces the following output:
// 66
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 12 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 12 elements using Fold
.
var sequence = Enumerable.Range(1, 12);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h, i, j, k, l) =>
a + b + c + d + e + f + g + h + i + j + k + l);
Console.WriteLine(result);
// This code produces the following output:
// 78
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 13 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 13 elements using Fold
.
var sequence = Enumerable.Range(1, 13);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m) =>
a + b + c + d + e + f + g + h + i + j + k + l + m);
Console.WriteLine(result);
// This code produces the following output:
// 91
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 14 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 14 elements using Fold
.
var sequence = Enumerable.Range(1, 14);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n) =>
a + b + c + d + e + f + g + h + i + j + k + l + m + n);
Console.WriteLine(result);
// This code produces the following output:
// 105
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 15 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 15 elements using Fold
.
var sequence = Enumerable.Range(1, 15);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) =>
a + b + c + d + e + f + g + h + i + j + k + l + m + n + o);
Console.WriteLine(result);
// This code produces the following output:
// 120
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|
Fold<T, TResult>(IEnumerable<T>, Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult>)
Returns the result of applying a function to a sequence of 16 elements.
Declaration
public static TResult Fold<T, TResult>(this IEnumerable<T> source, Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult> folder)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The sequence of items to fold. |
Func<T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, TResult> | folder | Function to apply to the elements in the sequence. |
Returns
Type | Description |
---|---|
TResult | The folded value returned by |
Type Parameters
Name | Description |
---|---|
T | Type of element in the source sequence |
TResult | Type of the result |
Remarks
This operator uses immediate execution and buffers as many items of the source sequence as necessary.
Examples
The following code example demonstrates how to apply a projection to a sequence of 16 elements using Fold
.
var sequence = Enumerable.Range(1, 16);
// Fold a sequence into a single value.
var result = sequence
.Fold((a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) =>
a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p);
Console.WriteLine(result);
// This code produces the following output:
// 136
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
InvalidOperationException |
|