SingleOrDefault(TSource) Method (IEnumerable(TSource))

Enumerable.SingleOrDefault<TSource> Method (IEnumerable<TSource>)

 

Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

Namespace:   System.Linq
Assembly:  System.Core (in System.Core.dll)

public static TSource SingleOrDefault<TSource>(
	this IEnumerable<TSource> source
)

Parameters

source
Type: System.Collections.Generic.IEnumerable<TSource>

An IEnumerable<T> to return the single element of.

Return Value

Type: TSource

The single element of the input sequence, or default(TSource) if the sequence contains no elements.

Type Parameters

TSource

The type of the elements of source.

Exception Condition
ArgumentNullException

source is null.

InvalidOperationException

The input sequence contains more than one element.

The default value for reference and nullable types is null.

The SingleOrDefault method does not provide a way to specify a default value. If you want to specify a default value other than default(TSource), use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method as described in the Example section.

The following code example demonstrates how to use SingleOrDefault<TSource>(IEnumerable<TSource>) to select the only element of an array.

string[] fruits1 = { "orange" };

string fruit1 = fruits1.SingleOrDefault();

Console.WriteLine(fruit1);

/*
 This code produces the following output:

 orange
*/

The following code example demonstrates that SingleOrDefault<TSource>(IEnumerable<TSource>) returns a default value when the sequence does not contain exactly one element.

string[] fruits2 = { };

string fruit2 = fruits2.SingleOrDefault();

Console.WriteLine(
    String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);

/*
 This code produces the following output:

 No such string!
*/

Sometimes the value of default(TSource) is not the default value that you want to use if the collection contains no elements. Instead of checking the result for the unwanted default value and then changing it if necessary, you can use the DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) method to specify the default value that you want to use if the collection is empty. Then, call Single<TSource>(IEnumerable<TSource>) to obtain the element. The following code example uses both techniques to obtain a default value of 1 if a collection of page numbers is empty. Because the default value for an integer is 0, which is not usually a valid page number, the default value must be specified as 1 instead. The first result variable is checked for the unwanted default value after the query has finished executing. The second result variable is obtained by using DefaultIfEmpty<TSource>(IEnumerable<TSource>, TSource) to specify a default value of 1.

int[] pageNumbers = { };

// Setting the default value to 1 after the query.
int pageNumber1 = pageNumbers.SingleOrDefault();
if (pageNumber1 == 0)
{
    pageNumber1 = 1;
}
Console.WriteLine("The value of the pageNumber1 variable is {0}", pageNumber1);

// Setting the default value to 1 by using DefaultIfEmpty() in the query.
int pageNumber2 = pageNumbers.DefaultIfEmpty(1).Single();
Console.WriteLine("The value of the pageNumber2 variable is {0}", pageNumber2);

/*
 This code produces the following output:

 The value of the pageNumber1 variable is 1
 The value of the pageNumber2 variable is 1
*/

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft