Element Operators: Single & SingleOrDefault

Element Operators Description
Single Returns the only element from a collection, or the only element that satisfies a condition. If Single() found no elements or more than one elements in the collection then throws InvalidOperationException.
SingleOrDefault The same as Single, except that it returns a default value of a specified generic type, instead of throwing an exception if no element found for the specified condition. However, it will thrown InvalidOperationException if it found more than one element for the specified condition in the collection.

Single and SingleOrDefault have two overload methods. The first overload method doesn't take any input parameter and returns a single element in the collection. The second overload method takes the lambda expression as a predicate delegate that specifies the condition and returns a single element that satisfies the specified condition.

Single() Á SingleOrDefault() Overloads:
public static TSource Single<TSource>(this IEnumerable<TSource> source);

public static TSource Single<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

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

public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

Single() returns the only element from a collection, or the only element that satisfies the specified condition. If a given collection includes no elements or more than one elements then Single() throws InvalidOperationException.

The SingleOrDefault() method does the same thing as Single() method. The only difference is that it returns default value of the data type of a collection if a collection is empty, includes more than one element or finds no element or more than one element for the specified condition.

Example: Single in method syntax C#
IList<int> oneElementList = new List<int>() { 7 };
IList<int> intList = new List<int>() { 7, 10, 21, 30, 45, 50, 87 };
IList<string> strList = new List<string>() { null, "Two", "Three", "Four", "Five" };
IList<string> emptyList = new List<string>();

Console.WriteLine("The only element in oneElementList: {0}", oneElementList.Single());
Console.WriteLine("The only element in oneElementList: {0}",
             oneElementList.SingleOrDefault());

Console.WriteLine("Element in emptyList: {0}", emptyList.SingleOrDefault());

Console.WriteLine("The only element which is less than 10 in intList: {0}",
             intList.Single(i => i < 10));

//Followings throw an exception
//Console.WriteLine("The only Element in intList: {0}", intList.Single());
//Console.WriteLine("The only Element in intList: {0}", intList.SingleOrDefault());
//Console.WriteLine("The only Element in emptyList: {0}", emptyList.Single());
Output:
The only element in oneElementList: 7
The only element in oneElementList: 7
Element in emptyList: 0
The only element which is less than 10 in intList: 7

The following example code throws an exception because Single() or SingleOrDefault() returns none or multiple elements for the specified condition.

C#: Single() and SingleOrDefault()
    IList<int> oneElementList = new List<int>() { 7 };
IList<int> intList = new List<int>() { 7, 10, 21, 30, 45, 50, 87 };
IList<string> strList = new List<string>() { null, "Two", "Three", "Four", "Five" };
IList<string> emptyList = new List<string>();

//following throws error because list contains more than one element which is less than 100
Console.WriteLine("Element less than 100 in intList: {0}", intList.Single(i => i < 100));

//following throws error because list contains more than one element which is less than 100
Console.WriteLine("Element less than 100 in intList: {0}", 
                                    intList.SingleOrDefault(i => i < 100));

//following throws error because list contains more than one elements
Console.WriteLine("The only Element in intList: {0}", intList.Single());

//following throws error because list contains more than one elements
Console.WriteLine("The only Element in intList: {0}", intList.SingleOrDefault());

//following throws error because list does not contains any element
Console.WriteLine("The only Element in emptyList: {0}", emptyList.Single());
Points to Remember :
  1. Single() expects one and only one element in the collection.
  2. Single() throws an exception when it gets no element or more than one elements in the collection.
  3. If specified a condition in Single() and result contains no element or more than one elements then it throws an exception.
  4. SingleOrDefault() will return default value of a data type of generic collection if there is no elements in a colection or for the specified condition.
  5. SingleOrDefault() will throw an exception if there is more than one elements in a colection or for the specified condition.