ExtensionMethod.NET Home of 877 C#, Visual Basic, F# and Javascript extension methods


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


public static T FirstOrNull<T>(this IEnumerable<T> values) where T: class
    return values.DefaultIfEmpty(null).FirstOrDefault();


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()
.Where(m => m.Name == "Name")

Author: Jason Briggs

Submitted on: 30 jun. 2015

Language: C#

Type: System.Collections.Generic.IEnumerable<T>

Views: 8956