---
title: "What are the LINQ Single(), and SingleOrDefault() functions?"  
description: "What are the LINQ Single(), and SingleOrDefault() functions?"  
author: "Amrith Chandran"  
published: 2025-02-12  
updated: 2025-10-24  
canonical: https://www.mindstick.com/forum/161157/what-are-the-linq-single-and-singleordefault-functions  
category: "linq"  
tags: ["linq", "linq function"]  
reading_time: 3 minutes  

---

# What are the LINQ Single(), and SingleOrDefault() functions?

What are the LINQ Single(), and [SingleOrDefault](https://www.mindstick.com/forum/34064/use-singleordefault-in-linq)() functions?

## Replies

### Reply by Ashutosh Patel

#### 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()

- 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-

```cs
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.

```cs
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-

```cs
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
   }
}
```

**Also, Read:** [Explain the ElementAt(), and ElementAtOrDefault() functions in LINQ with C#.](https://www.mindstick.com/forum/161156/explain-the-elementat-and-elementatordefault-functions-in-linq-with-c-sharp)


---

Original Source: https://www.mindstick.com/forum/161157/what-are-the-linq-single-and-singleordefault-functions

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
