public static TSource Last <TSource>(this IEnumerable <TSource> source) { if (source == null) { throw Error.ArgumentNull("source"); } IPartition <TSource> partition = source as IPartition <TSource>; if (partition != null) { return(partition.Last()); } IList <TSource> list = source as IList <TSource>; if (list != null) { int count = list.Count; if (count > 0) { return(list[count - 1]); } } else { using (IEnumerator <TSource> e = source.GetEnumerator()) { if (e.MoveNext()) { TSource result; do { result = e.Current; } while (e.MoveNext()); return(result); } } } throw Error.NoElements(); }