First/FirstOrDefault/Single/SingleOrDefault In LINQ

First/FirstOrDefault,Single/SingleOrDefault are types of Element Operators in Linq.
 
Element Operator returns single element from a sequence based on Index value or based on condition. They have an overloaded version that accepts a predicate.
 

First()

 
It has two overloaded versions. One overloaded version does not accept any parameter as input ,it returns the first element of the sequence and throws an InvalidOperation Exception if the sequence is empty.
 
Example 
  1. int[] num = {2,3,4,5,6,7,8}  
  2. Console.WriteLine(num.First())  
Output will be 2,
  1. int[] num = { }  
  2. Console.WriteLine(num.First())  
Will throw an InvalidOperation Exception
 
Second overloaded version of First() will return first element from sequence which satisfies the condition. It will throw an error InvalidOperation Exception if sequence is empty or no element is found that satisfies the condition.
 
Example 
  1. int[] num = {2,3,4,5,6,7,8}  
  2. Console.WriteLine(num.First(x => x % 4==0))  
Output will be 4
  1. int[] num = { }  
  2. Console.WriteLine(num.First(x => x % 4 == 0)) 
Will throw an InvalidOperation Exception
 
And
  1. int[] num = {2,3,4,5,6,7,8}  
  2. Console.WriteLine(num.First(x => x % 10==0))  
Will  also throw an InvalidOperation Exception
 

FirstOrDefault()

 
It also has two overloaded versions and works the same as First(), except it will not throw an error if sequence is empty or no element is found that
satisfies the condition. Instead it will return default values, for reference type default value is NULL and for value type the default value depends on actual type expected. 
 
Example 
  1. int[] num = { }  
  2. Console.WriteLine(num.FirstOrDefault(x => x % 4 == 0))  
Will return 0
  1. int[] num = {2,3,4,5,6,7,8}  
  2. Console.WriteLine(num.FirstOrDefault(x => x % 10==0)) 
Will return 0 
  1. string[] name = { "ABC""DE""XYZH""LMNO" };  
  2. Console.WriteLine(name.FirstOrDefault(x => x.Length > 5)); 
Output will be blank
 

Single()

 
It will return Single value from sequence of elements, and will throw an error, InvalidOperation Exception if the sequence is empty or sequence has more than one value.
 
Example
  1. int[] num = { 2, 4, 5, 6, 7, 8 };  
  2. Console.WriteLine(num.Single());  
Will throw InvalidOperation Exception  as Sequence has more than one value.
  1. int[] num = { 2};  
  2. Console.WriteLine(num.Single());  
Output will be 2 
  1. int[] num = { };  
  2. Console.WriteLine(num.Single());   
Will throw InvalidOperation exception  as sequence is empty.
 
Another overloaded version of Single() expects a condition. It will return single element which satisfies the condition.
 
Will throw an InvalidOperation exception if the sequence is empty or more than one element satisfies the condition or no element satisfies the condition.
 
Example
  1. int[] num = { 2, 4, 5, 6, 7, 8 };  
  2. Console.WriteLine(num.Single(x => x % 5 == 0));  
Output will be 5
  1. int[] num = { 2, 4, 5, 6, 7, 8 };  
  2. Console.WriteLine(num.Single(x => x % 2 == 0));   
Will throw an InvalidOperation exception as more than one element satisfies the condition.
  1. int[] num = { 2, 4, 5, 6, 7, 8 };  
  2. Console.WriteLine(num.Single(x => x % 10 == 0));    
Will throw an InvalidOperation exception as no element satisfies the condition. 
  1. int[] num = { };   
  2. Console.WriteLine(num.Single(x => x % 2 == 0));  
Will throwan InvalidOperation exception as sequence is empty
 

SingleOrDefault()

 
Same as Single and it has same overloaded versions as Single, except it will not throw an error if no element satisfies the condition or sequence is Empty. Instead it will return default value. But if the sequence has more than one value or more than one value satisfies the condition it will throw an error.
 
Example
  1. int[] num = { };  
  2. Console.WriteLine(num.SingleOrDefault());  
  3. Console.WriteLine(num.SingleOrDefault(x => x % 4 == 0));  
Output will be 0 as sequence is empty
  1. int[] num = { 2, 4, 5, 6, 7, 8 };   
  2. Console.WriteLine(num.SingleOrDefault(x => x % 10 == 0));  
Output will be 0 as no element satisfies the condition
  1. int[] num = { 2, 4, 5, 6, 7, 8 };  
  2. Console.WriteLine(num.SingleOrDefault(x => x % 2== 0));  
Just like single, it will throw InvalidOperation exception as more than one element satisfies the condition 
  1. int[] num = { 2, 4, 5, 6, 7, 8 };  
  2. Console.WriteLine(num.SingleOrDefault());   
Just like single, it will throw InvalidOperation exception as Sequence contains more than one element.