public static TSource FirstOrDefault <TSource>(this IEnumerable <TSource> source) { if (source == null) { throw Error.ArgumentNull("source"); } IPartition <TSource> partition = source as IPartition <TSource>; if (partition != null) { bool found; return(partition.TryGetFirst(out found)); } IList <TSource> list = source as IList <TSource>; if (list != null) { if (list.Count > 0) { return(list[0]); } } else { using (IEnumerator <TSource> e = source.GetEnumerator()) { if (e.MoveNext()) { return(e.Current); } } } return(default(TSource)); }
public TResult TryGetFirst(out bool found) { bool sourceFound; TSource input = _source.TryGetFirst(out sourceFound); found = sourceFound; return(sourceFound ? _selector(input) : default(TResult)); }
public static TSource First <TSource>(this IEnumerable <TSource> source) { if (source == null) { throw Error.ArgumentNull(nameof(source)); } IPartition <TSource> partition = source as IPartition <TSource>; if (partition != null) { bool found; TSource first = partition.TryGetFirst(out found); if (found) { return(first); } } else { IList <TSource> list = source as IList <TSource>; if (list != null) { if (list.Count > 0) { return(list[0]); } } else { using (IEnumerator <TSource> e = source.GetEnumerator()) { if (e.MoveNext()) { return(e.Current); } } } } throw Error.NoElements(); }
internal static TSource TryGetFirst <TSource>(this IEnumerable <TSource> source, out bool found) { if (source == null) { throw Error.ArgumentNull(nameof(source)); } IPartition <TSource> partition = source as IPartition <TSource>; if (partition != null) { return(partition.TryGetFirst(out found)); } IList <TSource> list = source as IList <TSource>; if (list != null) { if (list.Count > 0) { found = true; return(list[0]); } } else { using (IEnumerator <TSource> e = source.GetEnumerator()) { if (e.MoveNext()) { found = true; return(e.Current); } } } found = false; return(default(TSource)); }