public static TSource First <TSource>(IEnumerable <TSource> source) { if (source == null) { throw ThrowHelper.ArgumentNull("source"); } IList <TSource> list = source as IList <TSource>; if (list == null) { using (IEnumerator <TSource> enumerator = source.GetEnumerator()) { if (!enumerator.MoveNext()) { throw ThrowHelper.NoElements(); } else { return(enumerator.Current); } } } else if (list.Count > 0) { return(list[0]); } throw ThrowHelper.NoElements(); }