Single() and SingleOrDefault() are both used to retrieve a single, unique element from a collection, but they behave differently when the collection contains zero, one, or more than one element.
Single()
Returns only one element from the collection.
Throws an exception if:
The collection has zero elements (InvalidOperationException).
The collection has more than one matching element (InvalidOperationException).
Example-
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 5 };
int result = numbers.Single();
Console.WriteLine("Single element: " + result);
// Output: Single element: 5
//--------Throws Exception for Empty Collection----------
List<int> numbers2 = new List<int> { };
int result2 = numbers2.Single(); // No elements
// Throws: InvalidOperationException: "Sequence contains no elements"
//----------Throws Exception for Multiple Elements----------
List<int> numbers3 = new List<int> { 5, 10, 15 };
int result3 = numbers3.Single(); //More than one element
// Throws: InvalidOperationException: "Sequence contains more than one element"
}
}
SingleOrDefault()
Returns exactly one element from the collection.
If the collection is empty, it returns the default value (null for reference types,
0 for numeric types).
It also throws an exception if the collection has more than one matching element.
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 5 };
int result = numbers.SingleOrDefault();
Console.WriteLine("SingleOrDefault element: " + result);
// Output: SingleOrDefault element: 5
//-----------Returns Default for Empty Collection---------------
List<int> numbers2 = new List<int> { };
int result2 = numbers2.SingleOrDefault();
Console.WriteLine("SingleOrDefault element: " + result2);
// Output: SingleOrDefault element: 0 (default for int)
//---------------Throws Exception for Multiple Elements--------------
List<int> numbers3 = new List<int> { 5, 10, 15 };
int result3 = numbers3.SingleOrDefault(); // More than one element
// Throws: InvalidOperationException: "Sequence contains more than one element"
}
}
When to use what?
Use Single() when you expect only one element and want an exception if the count is false.
Use SingleOrDefault() when you expect zero or one element but want to handle an empty collection gracefully.
Final Example-
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3 };
// Using Single() (Will throw an exception if more than one element exists)
try
{
int value = numbers.Single(n => n > 1);
Console.WriteLine("Single element: " + value);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
// Using SingleOrDefault() (Returns 0 instead of exception when no match)
int safeValue = numbers.SingleOrDefault(n => n > 10);
Console.WriteLine("SingleOrDefault element: " + safeValue);
// Output: SingleOrDefault element: 0
}
}
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Single() vs SingleOrDefault() in LINQ (C#)
Single() and SingleOrDefault() are both used to retrieve a single, unique element from a collection, but they behave differently when the collection contains zero, one, or more than one element.
Single()
Example-
SingleOrDefault()
nullfor reference types,0for numeric types).When to use what?
Final Example-
Also, Read: Explain the ElementAt(), and ElementAtOrDefault() functions in LINQ with C#.