C# SingleOrDefault, SingleUse the SingleOrDefault and Single extension methods to get a single matching element if one exists.
SingleOrDefault. This C# method returns an element if a single one is present. With it you receive the single matching element, or the default value if no element is found.
Single. SingleOrDefault has one difference to Single. With Single, an exception is thrown when zero matching elements are found. But with SingleOrDefault, the default value is returned.
SingleOrDefault example. This program allocates an array of 3 integers. Next, the SingleOrDefault method is called. A Predicate is specified to search for a single element equal to 5.
Result No element with a value of 5 exists, so the default value of int is returned: 0.
Int Array
Next SingleOrDefault is called to find an element of value 1—one such element exists.
Finally SingleOrDefault is used to find an element >= 1. Because 3 elements match this condition, an exception is thrown.
InvalidOperation Exception
C# program that uses SingleOrDefault method
using System; using System.Linq; class Program { static void Main() { int[] array = { 1, 2, 3 }; // Default is returned if no element found. int a = array.SingleOrDefault(element => element == 5); Console.WriteLine(a); // Value is returned if one is found. int b = array.SingleOrDefault(element => element == 1); Console.WriteLine(b); try { // Exception is thrown if more than one is found. int c = array.SingleOrDefault(element => element >= 1); } catch (Exception ex) { Console.WriteLine(ex.GetType()); } } }
0 1 System.InvalidOperationException
Single example. This program creates an array of 5 ints. Only one of the elements is greater than 999. Next, Single is called with a Predicate lambda expression.
And After this, the parameterless Single method is called on an array with only one element.
And The Single method, when only one match is found, returns that one element.
C# program that uses Single method
using System; using System.Linq; class Program { static void Main() { // Find only element > 999. int[] array1 = { 1, 3, 1000, 4, 5 }; int value1 = array1.Single(element => element > 999); // Ensure only one element. int[] array2 = { 4 }; int value2 = array2.Single(); Console.WriteLine(value1); Console.WriteLine(value2); // See exception when more than one element found. try { int value3 = array1.Single(element => element > 0); } catch (Exception ex) { Console.WriteLine(ex.GetType()); } } }
1000 4 System.InvalidOperationException
Single, no match. What happens if no single matching element is found? Single() tries to find an element matching the predicate of > 0. There are 5 matching elements, so an exception is thrown.
Note The type of the exception is System.InvalidOperationException. This exception is used throughout the .NET Framework.
Discussion. Is Single a useful method? Unfortunately, methods that can throw an exception in a normal circumstance such as Single may be less useful.
So If your program must have a single instance of a certain element, then it can be used to alert you to serious errors.
Thus The SingleOrDefault() method is probably useful more often. It handles the no-result case more gracefully.
A summary. SingleOrDefault helps ensure zero or one elements exist matching a condition. As with Single, it can be used with no parameters to match all elements.
© 2007-2021 sam allen.
see site info on the changelog.