Ejemplo n.º 1
0
        public static TSource LastOrDefault <TSource>(this IEnumerable <TSource> source)
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }
            IPartition <TSource> partition = source as IPartition <TSource>;

            if (partition != null)
            {
                return(partition.LastOrDefault());
            }
            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);
                    }
                }
            }
            return(default(TSource));
        }