Method FallbackIfEmpty
| Edit this page View SourceFallbackIfEmpty<T>(IEnumerable<T>, params T[])
Returns the elements of a sequence, but if it is empty then returns an alternate sequence from an array of values.
Declaration
public static IEnumerable<T> FallbackIfEmpty<T>(this IEnumerable<T> source, params T[] fallback)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
T[] | fallback | The array that is returned as the alternate sequence if |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable<T> that containing fallback values if |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the sequences. |
Remarks
The length of source
is not evaluated until enumeration. source
is
enumerated; if there is at least one item, the elements of source
will be streamed in a
deferred manner. If there are no items in source
, the items in fallback
will be streamed.
Examples
The following code example demonstrates how to use FallbackIfEmpty
to use a fallback sequence if the primary sequence is empty.
var flag = false;
var sequence = SuperEnumerable.Defer(
() =>
{
if (flag) return Enumerable.Empty<int>();
flag = true;
return Enumerable.Range(1, 5);
});
// Replace a sequence if it is empty.
var result = sequence.FallbackIfEmpty(10, 11, 12);
Console.WriteLine(
"Non-Empty: [" +
string.Join(", ", result) +
"]");
Console.WriteLine(
"Empty: [" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// Non-Empty: [1, 2, 3, 4, 5]
// Empty: [10, 11, 12]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
FallbackIfEmpty<T>(IEnumerable<T>, IEnumerable<T>)
Returns the elements of a sequence, but if it is empty then returns an alternate sequence of values.
Declaration
public static IEnumerable<T> FallbackIfEmpty<T>(this IEnumerable<T> source, IEnumerable<T> fallback)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
IEnumerable<T> | fallback | The alternate sequence that is returned if |
Returns
Type | Description |
---|---|
IEnumerable<T> | An IEnumerable<T> that containing fallback values if |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the sequences. |
Remarks
The length of source
is not evaluated until enumeration. source
is
enumerated; if there is at least one item, the elements of source
will be streamed using
deferred execution. If there are no items in source
, then fallback
will be streamed using deferred execution.
Examples
The following code example demonstrates how to use FallbackIfEmpty
to use a fallback sequence if the primary sequence is empty.
var flag = false;
var sequence = SuperEnumerable.Defer(
() =>
{
if (flag) return Enumerable.Empty<int>();
flag = true;
return Enumerable.Range(1, 5);
});
// Replace a sequence if it is empty.
var result = sequence
.FallbackIfEmpty(
Enumerable.Range(20, 3));
Console.WriteLine(
"Non-Empty: [" +
string.Join(", ", result) +
"]");
Console.WriteLine(
"Empty: [" +
string.Join(", ", result) +
"]");
// This code produces the following output:
// Non-Empty: [1, 2, 3, 4, 5]
// Empty: [20, 21, 22]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|