Enumerable.SingleOrDefault Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a single, specific element of a sequence, or a default value if that element is not found.
Overloads
| Name | Description |
|---|---|
|
SingleOrDefault |
Returns the only element of a sequence that satisfies a specified condition, or a specified default value if no such element exists; this method throws an exception if more than one element satisfies the condition. |
|
SingleOrDefault |
Returns the only element of a sequence, or a specified default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence. |
|
SingleOrDefault |
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition. |
|
SingleOrDefault |
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. |
SingleOrDefault(IEnumerable, Func, TSource)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
Returns the only element of a sequence that satisfies a specified condition, or a specified default value if no such element exists; this method throws an exception if more than one element satisfies the condition.
public:
generic
[System::Runtime::CompilerServices::Extension]
static TSource SingleOrDefault(System::Collections::Generic::IEnumerable ^ source, Func ^ predicate, TSource defaultValue);
public static TSource SingleOrDefault(this System.Collections.Generic.IEnumerable source, Func predicate, TSource defaultValue);
static member SingleOrDefault : seq<'Source> * Func<'Source, bool> * 'Source -> 'Source
Public Function SingleOrDefault(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean), defaultValue As TSource) As TSource
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
-
IEnumerable
An IEnumerable
- predicate
-
Func
Boolean>
A function to test an element for a condition.
- defaultValue
- TSource
The default value to return if the sequence is empty.
Returns
The single element of the input sequence that satisfies the condition, or defaultValue if no such element is found.
Exceptions
source or predicate is null.
More than one element satisfies the condition in predicate.
Applies to
SingleOrDefault(IEnumerable, TSource)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
Returns the only element of a sequence, or a specified default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.
public:
generic
[System::Runtime::CompilerServices::Extension]
static TSource SingleOrDefault(System::Collections::Generic::IEnumerable ^ source, TSource defaultValue);
public static TSource SingleOrDefault(this System.Collections.Generic.IEnumerable source, TSource defaultValue);
static member SingleOrDefault : seq<'Source> * 'Source -> 'Source
Public Function SingleOrDefault(Of TSource) (source As IEnumerable(Of TSource), defaultValue As TSource) As TSource
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
-
IEnumerable
An IEnumerable
- defaultValue
- TSource
The default value to return if the sequence is empty.
Returns
The single element of the input sequence, or defaultValue if the sequence contains no elements.
Exceptions
source is null.
The input sequence contains more than one element.
Applies to
SingleOrDefault(IEnumerable, Func)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.
public:
generic
[System::Runtime::CompilerServices::Extension]
static TSource SingleOrDefault(System::Collections::Generic::IEnumerable ^ source, Func ^ predicate);
public static TSource SingleOrDefault(this System.Collections.Generic.IEnumerable source, Func predicate);
public static TSource? SingleOrDefault(this System.Collections.Generic.IEnumerable source, Func predicate);
static member SingleOrDefault : seq<'Source> * Func<'Source, bool> -> 'Source
Public Function SingleOrDefault(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As TSource
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
-
IEnumerable
An IEnumerable
- predicate
-
Func
Boolean>
A function to test an element for a condition.
Returns
The single element of the input sequence that satisfies the condition, or default(TSource) if no such element is found.
Exceptions
source or predicate is null.
More than one element satisfies the condition in predicate.
Examples
The following code example demonstrates how to use SingleOrDefault
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
string fruit1 = fruits.SingleOrDefault(fruit => fruit.Length > 10);
Console.WriteLine(fruit1);
/*
This code produces the following output:
passionfruit
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
' Get the single item in the array whose length is > 10.
Dim fruit1 As String =
fruits.SingleOrDefault(Function(fruit) fruit.Length > 10)
' Display the result.
Console.WriteLine($"First array: {fruit1}")
The following code example demonstrates that SingleOrDefault
string fruit2 =
fruits.SingleOrDefault(fruit => fruit.Length > 15);
Console.WriteLine(
String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);
/*
This code produces the following output:
No such string!
*/
' Get the single item in the array whose length is > 15.
Dim fruit2 As String =
fruits.SingleOrDefault(Function(fruit) fruit.Length > 15)
' Display the result.
Dim output As String =
IIf(String.IsNullOrEmpty(fruit2), "No single item found", fruit2)
Console.WriteLine($"Second array: {output}")
' This code produces the following output:
'
' First array: passionfruit
' Second array: No single item found
Remarks
The default value for reference and nullable types is null.
Applies to
SingleOrDefault(IEnumerable)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
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.
public:
generic
[System::Runtime::CompilerServices::Extension]
static TSource SingleOrDefault(System::Collections::Generic::IEnumerable ^ source);
public static TSource SingleOrDefault(this System.Collections.Generic.IEnumerable source);
public static TSource? SingleOrDefault(this System.Collections.Generic.IEnumerable source);
static member SingleOrDefault : seq<'Source> -> 'Source
Public Function SingleOrDefault(Of TSource) (source As IEnumerable(Of TSource)) As TSource
Type Parameters
- TSource
The type of the elements of source.
Parameters
- source
-
IEnumerable
An IEnumerable
Returns
The single element of the input sequence, or default(TSource) if the sequence contains no elements.
Exceptions
source is null.
The input sequence contains more than one element.
Examples
The following code example demonstrates how to use SingleOrDefault
string[] fruits1 = { "orange" };
string fruit1 = fruits1.SingleOrDefault();
Console.WriteLine(fruit1);
/*
This code produces the following output:
orange
*/
' Create an array that contains one item.
Dim fruits1() As String = {"orange"}
' Get the single item in the array or else a default value.
Dim result As String = fruits1.SingleOrDefault()
' Display the result.
Console.WriteLine($"First array: {result}")
The following code example demonstrates that SingleOrDefault
string[] fruits2 = { };
string fruit2 = fruits2.SingleOrDefault();
Console.WriteLine(
String.IsNullOrEmpty(fruit2) ? "No such string!" : fruit2);
/*
This code produces the following output:
No such string!
*/
' Create an empty array.
Dim fruits2() As String = {}
result = String.Empty
' Get the single item in the array or else a default value.
result = fruits2.SingleOrDefault()
' Display the result.
Dim output As String =
IIf(String.IsNullOrEmpty(result), "No single item found", result)
Console.WriteLine($"Second array: {output}")
' This code produces the following output:
'
' First array: orange
' Second array: No single item found
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
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
*/
Dim pageNumbers() As Integer = {}
' Setting the default value to 1 after the query.
Dim pageNumber1 As Integer = pageNumbers.SingleOrDefault()
If pageNumber1 = 0 Then
pageNumber1 = 1
End If
Console.WriteLine($"The value of the pageNumber1 variable is {pageNumber1}")
' Setting the default value to 1 by using DefaultIfEmpty() in the query.
Dim pageNumber2 As Integer = pageNumbers.DefaultIfEmpty(1).Single()
Console.WriteLine($"The value of the pageNumber2 variable is {pageNumber2}")
' This code produces the following output:
' The value of the pageNumber1 variable is 1
' The value of the pageNumber2 variable is 1
Remarks
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