---
title: "Understanding .Single()"  
description: "Understanding .Single()"  
author: "Anonymous User"  
published: 2014-08-23  
updated: 2014-08-23  
canonical: https://www.mindstick.com/forum/2097/understanding-single  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Understanding .Single()

Browsing [sample](https://www.mindstick.com/news/2174/mars-mission-by-nasa-prepares-for-tests) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) I'm seeing a [usage](https://www.mindstick.com/forum/155707/what-is-the-usage-of-asp-dot-net-configuration-file) I don't understand:

[var](https://www.mindstick.com/forum/33920/what-is-different-var-and-dynamic-types-in-c-sharp) orderRow = order.Rows.Single();

Rows is an enumerable and usually one would iterate using a [foreach loop](https://www.mindstick.com/forum/182/problem-in-getting-the-value-from-array-by-using-foreach-loop). Is the .Single a workaround for cases where, for whatever reason, the foreach can't be (or need not) be used?

var [option](https://www.mindstick.com/forum/159391/jquery-get-selected-option-from-dropdown) = options.OfType<AdditionalLocationsOption>().SingleOrDefault();

if (option != [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp)){

...stuff

}

## Replies

### Reply by Sumit Kesarwani

Hi Chintoo, \

LINQ's Single method returns the single element in a collection.

This would be written in Rows is known to contain exactly one row.

If it's empty, or if it has more than one row, an exception will be thrown.


---

Original Source: https://www.mindstick.com/forum/2097/understanding-single

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
