Click or drag to resize

MoreEnumerableSingleOrFallbackTSource 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.4.0
[ObsoleteAttribute("Consider using FallbackIfEmpty instead. SingleOrFallback may be removed in a future version. For more information, see")]
public static TSource SingleOrFallback<TSource>(
	this IEnumerable<TSource> source,
	Func<TSource> fallback


Type: System.Collections.GenericIEnumerableTSource
The source sequence
Type: SystemFuncTSource
The fallback delegate to execute if the sequence is empty

Type Parameters

Element type of sequence

Return Value

Type: TSource
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 IEnumerableTSource. 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).
ArgumentNullExceptionsource or fallback is null
InvalidOperationExceptionThe sequence has more than one element
The fallback delegate is not executed if the sequence is non-empty. This operator uses immediate execution and has optimizations for IListT sources.
var numbers = { 123, 456, 789 };
var result = numbers.Where(x => x == 100).SingleOrFallback(() => -1);
The result variable will contain -1.
See Also