Exemplo n.º 1
0
            public TResult TryGetLast(out bool found)
            {
                bool    sourceFound;
                TSource input = _source.TryGetLast(out sourceFound);

                found = sourceFound;
                return(sourceFound ? _selector(input) : default(TResult));
            }
Exemplo n.º 2
0
        public static TSource Last <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 last = partition.TryGetLast(out found);
                if (found)
                {
                    return(last);
                }
            }
            else
            {
                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();
        }
Exemplo n.º 3
0
        internal static TSource TryGetLast <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.TryGetLast(out found));
            }

            IList <TSource> list = source as IList <TSource>;

            if (list != null)
            {
                int count = list.Count;
                if (count > 0)
                {
                    found = true;
                    return(list[count - 1]);
                }
            }
            else
            {
                using (IEnumerator <TSource> e = source.GetEnumerator())
                {
                    if (e.MoveNext())
                    {
                        TSource result;
                        do
                        {
                            result = e.Current;
                        }while (e.MoveNext());

                        found = true;
                        return(result);
                    }
                }
            }

            found = false;
            return(default(TSource));
        }