Example #1
0
        public static TSource ElementAt <TSource>(this IEnumerable <TSource> source, int index)
        {
            if (source == null)
            {
                throw Error.ArgumentNull("source");
            }
            IPartition <TSource> partition = source as IPartition <TSource>;

            if (partition != null)
            {
                return(partition.ElementAt(index));
            }
            IList <TSource> list = source as IList <TSource>;

            if (list != null)
            {
                return(list[index]);
            }
            if (index >= 0)
            {
                using (IEnumerator <TSource> e = source.GetEnumerator())
                {
                    while (e.MoveNext())
                    {
                        if (index == 0)
                        {
                            return(e.Current);
                        }
                        index--;
                    }
                }
            }
            throw Error.ArgumentOutOfRange("index");
        }