Exemple #1
0
        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));
        }
Exemple #2
0
            public TResult TryGetFirst(out bool found)
            {
                bool    sourceFound;
                TSource input = _source.TryGetFirst(out sourceFound);

                found = sourceFound;
                return(sourceFound ? _selector(input) : default(TResult));
            }
Exemple #3
0
        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();
        }
Exemple #4
0
        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));
        }