FirstOrNull

public static T FirstOrNull<T>(this IEnumerable<T> values) where T: class
{
    return values.DefaultIfEmpty(null).FirstOrDefault();
}
Example:
using System.Reflection;
// in this example the MemberInfo for this.Name will be returned or 
// null if "this" doesn't have a member called Name 
var memberInfo = this.GetType()
    .GetMembers()
    .Where(m => m.Name == "Name")
    .FirstOrNull();

Description

Returns the null when there's null first element in the sequence instead of throwing an exception

Details

Double click on the code to select all.

 

;