Method Flatten
| Edit this page View SourceFlatten(IEnumerable)
Flattens a sequence containing arbitrarily-nested sequences.
Declaration
public static IEnumerable<object?> Flatten(this IEnumerable source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable | source | The sequence that will be flattened. |
Returns
Type | Description |
---|---|
IEnumerable<object> | A sequence that contains the elements of |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to flatten hierarchical sequences into a flat sequences using Flatten
.
var sequence = new object[]
{
1, 2, 3,
new object[]
{
4, 5,
new object[] { 6, 7, },
8, 9, 10,
},
11,
new object[]
{
12, 13, 14,
new object[] { 15, 16, 17, },
18, 19,
},
20,
};
// Flatten a hierarchical sequence
var result = sequence.Flatten();
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Flatten(IEnumerable, Func<IEnumerable, bool>)
Flattens a sequence containing arbitrarily-nested sequences. An additional parameter specifies a predicate function used to determine whether a nested IEnumerable should be flattened or not.
Declaration
public static IEnumerable<object?> Flatten(this IEnumerable source, Func<IEnumerable, bool> predicate)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable | source | The sequence that will be flattened. |
Func<IEnumerable, bool> | predicate | A function that receives each element that implements IEnumerable and indicates if its elements should be recursively flattened into the resulting sequence. |
Returns
Type | Description |
---|---|
IEnumerable<object> | A sequence that contains the elements of |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to flatten hierarchical sequences into a flat sequences using Flatten
.
var sequence = new object[]
{
1, 2, 3,
new object[]
{
4, 5,
new int[] { 6, 7, 8, },
},
9, 10, 11,
new object[]
{
12, 13, 14,
new object[] { 15, 16, 17, },
18, 19,
},
20,
};
// Flatten a hierarchical sequence
var result = sequence
.Flatten(e => e is not int[]);
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, 4, 5, System.Int32[], 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Flatten(IEnumerable, Func<object?, IEnumerable?>)
Flattens a sequence containing arbitrarily-nested sequences. An additional parameter specifies a function that projects an inner sequence via a property of an object.
Declaration
public static IEnumerable<object?> Flatten(this IEnumerable source, Func<object?, IEnumerable?> selector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable | source | The sequence that will be flattened. |
Func<object, IEnumerable> | selector | A function that receives each element of the sequence as an object and projects an inner sequence to be flattened. If the function returns null then the object argument is considered a leaf of the flattening process. |
Returns
Type | Description |
---|---|
IEnumerable<object> | A sequence that contains the elements of |
Remarks
This method uses deferred execution and streams its results.
Examples
The following code example demonstrates how to flatten hierarchical sequences into a flat sequences using Flatten
.
var sequence = new object[]
{
true,
false,
1,
"bar",
new object[]
{
2,
new[]
{
3,
},
},
'c',
4,
};
// Flatten a hierarchical sequence
var result = sequence
.Flatten(obj =>
obj switch
{
int => null,
IEnumerable inner => inner,
_ => Enumerable.Empty<object>(),
});
Console.WriteLine(
"[" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// [1, 2, 3, 4]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|