MoreEnumerable.SingleOrFallback<TSource> Method |
Note: This API is now obsolete.
Returns the single element in the given sequence, or the result
of executing a fallback delegate if the sequence is empty.
This method throws an exception if there is more than one element in the sequence.
Namespace:
MoreLinq
Assembly:
MoreLinq (in MoreLinq.dll) Version: 2.5.0
Syntax[ObsoleteAttribute("Consider using FallbackIfEmpty instead. SingleOrFallback may be removed in a future version. For more information, see https://github.com/morelinq/MoreLINQ/issues/122.")]
public static TSource SingleOrFallback<TSource>(
this IEnumerable<TSource> source,
Func<TSource> fallback
)
<ExtensionAttribute>
<ObsoleteAttribute("Consider using FallbackIfEmpty instead. SingleOrFallback may be removed in a future version. For more information, see https://github.com/morelinq/MoreLINQ/issues/122.")>
Public Shared Function SingleOrFallback(Of TSource) (
source As IEnumerable(Of TSource),
fallback As Func(Of TSource)
) As TSource
public:
[ExtensionAttribute]
[ObsoleteAttribute(L"Consider using FallbackIfEmpty instead. SingleOrFallback may be removed in a future version. For more information, see https://github.com/morelinq/MoreLINQ/issues/122.")]
generic<typename TSource>
static TSource SingleOrFallback(
IEnumerable<TSource>^ source,
Func<TSource>^ fallback
)
[<ExtensionAttribute>]
[<ObsoleteAttribute("Consider using FallbackIfEmpty instead. SingleOrFallback may be removed in a future version. For more information, see https://github.com/morelinq/MoreLINQ/issues/122.")>]
static member SingleOrFallback :
source : IEnumerable<'TSource> *
fallback : Func<'TSource> -> 'TSource
Parameters
- source
- Type: System.Collections.Generic.IEnumerable<TSource>
The source sequence - fallback
- Type: System.Func<TSource>
The fallback delegate to execute if the sequence is empty
Type Parameters
- TSource
- Element type of sequence
Return Value
Type:
TSourceThe single element in the sequence, or the result of calling the
fallback delegate if the sequence is empty.
Usage Note
In Visual Basic and C#, you can call this method as an instance method on any object of type
IEnumerable<TSource>. When you use instance method syntax to call this method, omit the first parameter. For more information, see
Extension Methods (Visual Basic) or
Extension Methods (C# Programming Guide).
Exceptions
Remarks
The fallback delegate is not executed if the sequence is non-empty.
This operator uses immediate execution and has optimizations for
IList< T> sources.
Examplesvar numbers = { 123, 456, 789 };
var result = numbers.Where(x => x == 100).SingleOrFallback(() => -1);
The
result variable will contain
-1.
See Also