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.
Declaration Syntax
C# | Visual Basic | Visual C++ |
public static TSource SingleOrFallback<TSource>( this IEnumerable<TSource> source, Func<TSource> fallback )
<ExtensionAttribute> _ Public Shared Function SingleOrFallback(Of TSource) ( _ source As IEnumerable(Of TSource), _ fallback As Func(Of TSource) _ ) As TSource
public: [ExtensionAttribute] generic<typename TSource> static TSource SingleOrFallback( IEnumerable<TSource>^ source, Func<TSource>^ fallback )
Generic Template Parameters
- TSource
- Element type of sequence
Parameters
- source (IEnumerable<(Of <(<'TSource>)>)>)
- The source sequence
- fallback (Func<(Of <(<'TSource>)>)>)
- The fallback delegate to execute if the sequence is empty
Return Value
The 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<(Of <(<'TSource>)>)>. When you use instance method syntax to call this method, omit the first parameter.
Remarks
The fallback delegate is not executed if the sequence is non-empty.
This operator uses immediate execution and has optimizations for IList<(Of <(<'T>)>)> sources.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | source or fallback is null |
InvalidOperationException | The sequence has more than one element |
Assembly: MoreLinq (Module: MoreLinq.dll) Version: 1.0.16006.0 (1.0.16006.1845)