Method TrySingle
| Edit this page View SourceTrySingle<T, TCardinality>(IEnumerable<T>, TCardinality, TCardinality, TCardinality)
Returns a tuple with the cardinality of the sequence and the single element in the sequence if it contains exactly one element. similar to Single<TSource>(IEnumerable<TSource>).
Declaration
public static (TCardinality Cardinality, T? Value) TrySingle<T, TCardinality>(this IEnumerable<T> source, TCardinality zero, TCardinality one, TCardinality many)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
TCardinality | zero | The value that is returned in the tuple if the sequence has zero elements. |
TCardinality | one | The value that is returned in the tuple if the sequence has a single element only. |
TCardinality | many | The value that is returned in the tuple if the sequence has two or more elements. |
Returns
Type | Description |
---|---|
(TCardinality Cardinality, T Value) | A tuple containing the identified |
Type Parameters
Name | Description |
---|---|
T | The type of the elements of |
TCardinality | The type that expresses cardinality. |
Remarks
This operator uses immediate execution, but never consumes more than two elements from the sequence.
Examples
The following code example demonstrates how to evaluate the cardinality of a sequence using TrySingle
.
// Determine cardinality of sequence
var result = Enumerable.Range(1, 0).TrySingle("none", "one", "many");
Console.WriteLine(result.ToString());
result = Enumerable.Range(1, 1).TrySingle("none", "one", "many");
Console.WriteLine(result.ToString());
result = Enumerable.Range(1, 10).TrySingle("none", "one", "many");
Console.WriteLine(result.ToString());
// This code produces the following output:
// (none, 0)
// (one, 1)
// (many, 0)
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
TrySingle<T, TCardinality, TResult>(IEnumerable<T>, TCardinality, TCardinality, TCardinality, Func<TCardinality, T?, TResult>)
Returns a result projected from the the cardinality of the sequence and the single element in the sequence if it contains exactly one element.
Declaration
public static TResult TrySingle<T, TCardinality, TResult>(this IEnumerable<T> source, TCardinality zero, TCardinality one, TCardinality many, Func<TCardinality, T?, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<T> | source | The source sequence. |
TCardinality | zero | The value that is passed as the first argument to |
TCardinality | one | The value that is passed as the first argument to |
TCardinality | many | The value that is passed as the first argument to |
Func<TCardinality, T, TResult> | resultSelector |
|
Returns
Type | Description |
---|---|
TResult | The value returned by |
Type Parameters
Name | Description |
---|---|
T | The type of the elements of |
TCardinality | The type that expresses cardinality. |
TResult | The type of the result value returned by the |
Remarks
This operator uses immediate execution, but never consumes more than two elements from the sequence.
Examples
The following code example demonstrates how to evaluate the cardinality of a sequence using TrySingle
.
// Determine cardinality of sequence
var result = Enumerable.Range(10, 0).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" });
Console.WriteLine(result);
result = Enumerable.Range(10, 1).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" });
Console.WriteLine(result);
result = Enumerable.Range(10, 10).TrySingle(0, 1, 2, (count, one) => count switch { 0 => "no elements", 1 => $"single({one})", 2 => "many elements" });
Console.WriteLine(result);
// This code produces the following output:
// no elements
// single(10)
// many elements
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
TrySingle<TSource>(IEnumerable<TSource>)
Returns the single element in the sequence if it contains exactly one element. Similar to SingleOrDefault<TSource>(IEnumerable<TSource>).
Declaration
public static TSource? TrySingle<TSource>(this IEnumerable<TSource> source)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | The source sequence. |
Returns
Type | Description |
---|---|
TSource | The single element or the default value of |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |